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

Access methods of a class from a control template in a resource dictionary
I want to compare two time values in the format of HR:MIN:SEC. of the type string in c#?
A repository, according to the Repository pattern, should provide queries or actual entities?
Execute command does not fire in resource dictionary code behind
Raising events in C# from classes
External Alias vs. Reflection vs. System.Addin For Multi-DLL Testing?
Is the drawing surface of the Graphics object in PaintEventArgs relative to the ClipRectangle?
xsd:import inside an Embedded Resource XSD
How likely is it to get a HashCode collision with this hashcode function?
Can't resolve project reference because of indirect dependency on current target framework
What would be considered a VB.NET module in C#?
What is the value of binary 100…(31 zeros) for 32-bit integer?
Showing a wait dialog while doing interface operations in C#
How to bind/merge an exe into my program?
Activator.CreateInstance is unable to find a constructor
asp.net - gridview update refresh page and reload data

Categories

HOME
stata
ftp
typo3
locale
python-requests
android-ndk
bigtable
ebay-api
highmaps
watson-iot
artifactory
confluence-rest-api
jquery-select2
handlebars.js
rmarkdown
yacc
rtc
pyodbc
spring-data-solr
wowza
fine-uploader
android-notifications
hql
virtuemart
descriptor
xna
polybase
gnu-screen
priority-queue
traveling-salesman
cloudformation
large-files
typeclass
jms-topic
deferred
spring-oauth2
favorites
availability
perf
multi-user
abide
dotnetnuke-module
motion
jslider
cowboy
office-interop
aura-framework
jquery-ui-slider
uitextview
text-extraction
grails-2.0
spring-ide
openidm
htop
kendo-scheduler
critical-section
sqlproj
android-tabs
zsh-completion
strtol
virtualenvwrapper
mate-desktop
qtruby
sbt-proguard
mod
jquery-draggable
wonderware
ewam
skscene
angular-promise
windows-phone-8-emulator
exchange-server-2007
vspackage
android-networking
wingdb
frisby.js
pyalgotrade
banana-pi
satisfiability
traceur
sqldatasource
new-operator
google-voice
myrrix
fade
kohana-orm
proj4
http-compression
nx
zepto
m3u
event-propagation
pdf-reader
eeprom
advanceddatagrid
project-files
automapper-2
windows-xp-embedded
mismatch
sessiontracking
service-layer
n2
file-icons
cstring

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile