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

WPF: ListView SelectedItem binding works for a split second, then changes back
linq XML Sum IEnumerable<string>
Why are my “replacement parameters” getting transformed into empty strings?
Convert.ToInt32(String) in C# not working?
Most optimal way to model a generic class in nHibernate
How to declare an static instance of a Class in C++?
Get ASP.NET temporary folder path
Will a single task in C# be executed in parallel on a multi-core system? [closed]
Dynamically creating App Bar in Windows Phone 8 PhoneGap app
Report thread progress with a stage first approach
Adding value from html page on database
CefGlue How to get HTML source?
Action on SharePoint New Item form in selenium 2 using C# is not working
Clickonce Wpf Embed .NET 4.5
Error while Converting Date Using Convert.todatetime function.in asp.net
Browse dictionaries with params in C #

Categories

HOME
oauth-2.0
asp.net
ftp
ns2
cluster-analysis
phantomjs
readdir
ebay-api
azure-database-mysql
okhttp3
redhawksdr
ubuntu-14.04
zabbix
install
clip-path
vsixmanifest
rtc
box-api
angularjs-ng-repeat
phpexcel
ios-universal-links
adroitlogic
virtualenv
ng2-charts
fine-uploader
yui
swift3.1
aggregate
dompdf
smooth-scrolling
project-structure
restructuredtext
combinations
pushbullet
rvm
spock
pushpad
kundera
folder
slim-lang
lighting
azure-storage-files
rythm
instances
libzip
grails-2.0
serverless-architecture
google-chrome-storage
emacs24
openidm
cancan
vertex
totalview
business-catalyst
chain
mavlink
virtualenvwrapper
class-design
init
audiorecord
jsondoc
controlpanel
pushwoosh
bootstrap-tabs
properties-file
photogrammetry
redmine-plugins
ios-enterprise
visual-c#-express-2010
amzi-prolog
sqldatasource
slimscroll
starling-framework
data-layers
android-jack-and-jill
stringstream
infinity.js
removeall
bho
metapost
systrace
eeprom
pinchzoom
stl-algorithm
towerjs
fileoutputstream
visual-leak-detector
backlight
gmagick
method-call
designated-initializer
cpack
norton
simultaneous
miniport
linq-to-mysql
for-xml
glassfish-embedded
hp-trim
pixel-bender
html-help-workshop

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile