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

how to group a text by a number of lines
Azure Storage 403 Forbidden exception a 32-bit issue?
EXT.NET Dynamically created ComboBox and Store
C# - Processlist caching
Index Out Of Range Exception: List not working with add range
Azure StartCopyBlob only copied by Uri?
Delegate Closure with no memory allocation
How to replicate the window transparency effect found in the new DropBox UWP app
ASP.NET MVC 5 External Login not working in Live but working in localhost
C# Regular Deserialization only selects first object instance from Binary File
datagridview population in csharp not working
How to enclose label c#
ASP.net download file Using HttpContext.Current.Response fileName
i want to Not16 bits. but the answer is wrong.Why? [closed]
SA1401 Fields must be declared with private access. Use properties to expose fields
How validate XML with XSD, when part of validation rules are in WSDL

Categories

HOME
asterisk
http
jenkins-pipeline
aix
intellisense
gap-system
mifare
arguments
collision-detection
handlebars.js
digital
select2
mongoimport
elastalert
resharper
luis
taxonomy
views
policy
android-wear-2.0
deployment-descriptor
continuous-fourier
orgchart
ballerinalang
gulp-jshint
angular2-changedetection
hierarchy
semantic-ui-react
paper-trail-gem
arp
wercker
imageprocessor
javax
certbot
repeat
leaderboard
mediastream
cowboy
electron-packager
java-5
quartz
compiler-warnings
azure-data-catalog
import.io
visualizer
openpgp.js
reloaddata
business-catalyst
node.js-client
disassembly
group
android-tabs
easy68k
access-violation
weblogic-maven-plugin
stdlist
methodology
oryx
clear
github-for-mac
skscene
javapackager
jain-sip
adempiere
page-flipping
gmaps4jsf
perfect-scrollbar
dockerpy
random-access
uiblureffect
breeze-sharp
gnip
visual-c#-express-2010
amzi-prolog
rails-migrations
android-relativelayout
slimscroll
nomachine
xtify
complete
code-complete
pechkin
ironworker
typography
usersettings
wpd
cocotron
magicsuggest
libx264
github-archive
jmesa
sqlbuddy
inserthtml
cgaffinetransform
jqzoom
clgeocoder
isgl3d
pex-and-moles
object-tag
functional-specifications
netduino
n2
norton
lgpl
activescaffold
p4.net
pitch-shifting
large-data-volumes
hp-trim
ihtmldocument2
cstring

Resources

Encrypt Message