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

data type mismatch in criteria expression in c# 2010
Skip DataMember via DataContractSerializer
ServicedComponent Com proxy eventually fails
Embedded scripting on the AppStore - use Python, or snake my way in?
Using Expression to call a property and object and determine if the object is null or not
LINQ joining values from different classes
wp7 xna gestures not working
New window replicates
ASP.net C# Return JS Alert from another thread
Asp.net C# Webform forms Generator/Builder [closed]
Lambda event handler scope
Get OS-Version in WinRT Metro App C#
Enumerating DataPoints in view
Switching control backColor fast
How can a view fill a model?
Iterating form controls that are created at runtime

Categories

HOME
excel-formula
itunesconnect
domain-driven-design
sip
cors
popover
esxi
grid
mediarecorder
aggregation-framework
kendo-asp.net-mvc
wavefront
genexus
software-collections
google-search-appliance
core-data
fetch
osclass
point-of-sale
fastreport
jsfiddle
dompdf
nsoperation
web-parts
alert
beacon
hl7-fhir
webgl-earth
ckeditor4.x
pixels
android-cursorloader
libxml2
marionette
resx
uibarbuttonitem
checkmarx
sqldependency
restangular
osgi-bundle
kundera
ios-autolayout
jql
slim-lang
client-server
motion
file-conversion
compiled-query
java-5
serverless-architecture
google-chrome-storage
geminabox
remap
stream-socket-client
jsfl
dynamic-linking
sharpdevelop
fluent-nhibernate-mapping
gige-sdk
jtextarea
esprima
instaparse
init
system-integration
jmh
jcalendar
google-app-engine-php
nonblocking
imageicon
winforms-interop
eaaccessory
dcg
clearquest
system.diagnostics
data-quality-services
teamstudio-unplugged
quickfixn
iphone-6
method-overloading
unityvs
illegalargumentexception
api-eveonline
cocos2d-x-2.x
dotcmis
atg-dynamo
reactjs-native
android-relativelayout
nomachine
mdp
autofilter
ie-developer-tools
windows-scheduler
pageload
blackberry-eclipse-plugin
xpolog
file-processing
http-response-codes
morfik
file-storage
hotfix

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App