c#


Persist Object - Entity Framework


I am newbie in Entity Framework. I think I have a simple doubt, but I have not found the answer in other posts.
I have:
public class A_B
{
private int Id;
private string name;
private A a;
private B b;
}
public class A
{
private int Id;
private string name;
}
public class B
{
private int Id;
private string name;
}
When I ask entity framework to create object A_B in database, entity framework creates A_B, A and B in database too. I do not want to create objects A and B in the database. A and B are already in the database. I want to save only A_B.
The command to create A_B in the database is:
context.Entry(A_B).State = EntityState.Modified;
or
context.Entry(A_B).State = EntityState.Added;
DbContext.SaveChanges();
Thanks for your help!
Entity Framework are creating the existing objects because are untracked on the context, so the context are creating the entities on database.
You need to track that objects changing state of A and B to Modified to attach to current context and avoid the creation on db.
You can build your code based on your database if it is already built/partially built. Add the nuget packages for Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.SqlServer (assuming you're using SQL Server), and Microsoft.EntityFrameworkCore.Tools. Then, you can run a command similar to the below to automatically generate the code.
Scaffold-DbContext "Server=[servername];Database=[databasename];Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Test
Check out this course for a good introduction to Entity Framework.

Related Links

Equivalent WinForm DataGridView code to WPF DataGrid
Expander header event click
Websockets for newbie C# : catch answer in a variable
Simple.Data in ASP.NET Core - 'No Ado Provider found.'
C# Read/write stream async from console application
make a item list with a mysql database
C# Check for implementation of generic interface
Collision and direction in Unity C#…?
Conflicting Controller actions in Asp Net Core
the name 'lanesList' does not exist in the current context
What is the equivalent of Python pbkdf2 usage in C# for migrating password hashes
What does the output of Rfc2898DeriveBytes depend on and how should the salt be treated?
Play 360 Stereoscopic video with VideoPlayer
Improve code with Factory Pattern and DI
Get the difference years between 2 objects c# [duplicate]
C#: I get CS1002 and CS1525 error because of qoutes - how to avoid that? [duplicate]

Categories

HOME
asterisk
excel-formula
grizzly
itunesconnect
typo3
gsm
lithium
esxi
overrides
virtualhost
wkhtmltopdf
theory
mongoimport
google-search-appliance
velocity
email-attachments
oxid
mbed
tmux
box2d
pylint
amazon-data-pipeline
openam
airbnb
react-jsx
single-page-application
datasource
descriptor
git-branch
dev-c++
beaker-notebook
concatenation
traveling-salesman
mongoose-im
angular2-changedetection
csproj
deferred
overflow
wercker
directions
jcr
dropdownbox
http-proxy
border-layout
device-driver
strategy-pattern
usernotifications
julia-jump
ninja-forms
ssdp
rbac
sknode
irr
head
logan-square
togglebutton
spring-data-commons
dna-sequence
playframework-1.x
activesupport
richtext
multi-select
papyrus
vga
normalisation
phpdesigner
jmh
treelist
web-optimization
nugetgallery
salesforce-service-cloud
trendline
skbio
methodology
jersey-test-framework
sqldataadapter
wiql
notice
dpkt
firewire
stderr
genome
google-experiments
crtdbg.h
dojox.grid
oxygenxml
ant-contrib
code-smell
email-headers
cvi
node-mongodb-native
android-jack-and-jill
nomachine
php-amqplib
misfire-instruction
caroufredsel
usersettings
android-library
magicsuggest
brew
orchestration
blackberry-eclipse-plugin
pdf-reader
nserror
socketasynceventargs
animationdrawable
motorola-droid
maven-eclipse-plugin
virtual-printer
recess
funambol
marathontesting
lgpl
dongle
numerical-computing
expander
scalable

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App