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 Transition to new DialogStack in the middle of a FormDialog
AVG blocks me from using Console.ReadKey() in C# Visual Studio 2017?
XslCompiledTransform in ASP CORE
ASP Create own Update Statement in sqlDataSource
How do I integrate APIs from (https://www.bittrex.com/Home/Api) into my blog using C# [on hold]
C# streaming drone video
Create a context menu strip with vsPackage in Visual Studio
Nunit and console application results mismatched?
C++ Dynamic Link Library for both java and C#
C# Only allowing qr code to be scanned once
Create Multi line chart from datatable in WindowsForm C#
Switching between word documents interop
Checking whether a sequence of integers is increasing
C# CF - Convert a SOAP Message from a Stream
Breaking change in method overload resolution in C# 6 - explanation?
#Url.RouteUrl returns misleading/wrong path for a controller-action (unless the 'routeName' parameter is explicitly specified)

Categories

HOME
joomla
ssms
alasql
reference
readdir
coordinates
ms-application-insights
aggregate-functions
samsung-gear-s2
yacc
email-attachments
cakephp-2.0
android-notifications
minimum
versioning
web-parts
beacon
audio-streaming
datasource
coccinelle
gnu-screen
bootstrap-accordion
pixels
android-cursorloader
smart-device
utc
multilanguage
tikz
slim-lang
visual-prolog
asp.net-webhooks
ui-grid
glide-image-library
aspnetboilerplate
flume-ng
rivets.js
geminabox
opos
beanshell
seyren
rmq
scrollmagic
msiexec
nimbus
avi
std
serial-communication
cyberduck
firewire
transparentproxy
targetprocess
fileinputstream
powerpoint-2010
dotcmis
nest-initiative
rpt
new-operator
logo-lang
ojb
backbone-views
mosix
pudb
junction-table
servicemanager
mail-gem
netzke
jquery-ui-map
nsscrollview
scsf
jqzoom
clgeocoder
parallel-python
vectorwise
nbehave
netbeans7.0
onunload
jquery-attributes
miniport
bootstrap-protocol
high-load

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