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 list domains running code from a non-domain computer?
Programmatically search formatted text in Word document and surround with tags
ASP.NET MVC 5 + Owin + SimpleInjector
JSON element reader .NET
An object reference is required for the non-static field, method, or property webBrowser1
Entity Framework Code First reference a common database
How to Consume a Web Service that requires Login Credentials
Inline Parameterized Query Timing out
NHibernate “error dehydrating property value for…” “object does not match target type”
C# How to change FileAttributes from Normal to Directory
Fill one row of a two-dimensional array with a single-dimensional array
NUnit result are different in debug and release
Using TempData inside ASP.NET SignalR Hub
SqlClient count by date
A fast random generator in c# [closed]
C# Encapsulation (OOP) [duplicate]

Categories

HOME
swift3
alasql
process
pelican
microcontroller
apache-storm
nltk
terminal
python-3.4
rethinkdb
sahi
pyodbc
olap
angular-universal
mamp
web-worker
md2
loader
autologin
caldav
prepros
v4l2
apdu
aspxgridview
android-wear-2.0
openoffice-impress
ng2-bootstrap
oracle-nosql
webspeech-api
cache-control
javafxports
leaderboard
strategy-pattern
scip
jquery-inputmask
directadmin
test-kitchen
qt-quick
python-venv
ocpjp
rivets.js
nao-robot
noise
dcom
visio-2010
aws-rds
activesupport
sharpssh
sp
multi-select
esprima
therubyracer
android-tabs
deck.js
tinybox2
vga
objectify
boost-bind
shtml
3d-rendering
mod
bbedit
android-progressbar
nugetgallery
ooad
oryx
dday
android-contentresolver
kramdown
newlib
traminer
musicbrainz
firewire
stereotype
page-refresh
cakephp-2.6
ultraedit
bungeecord
strcpy
teamstudio-unplugged
crtdbg.h
fiware-health
system.io.packaging
diffmerge
famous-engine
ios-enterprise
scjp
connections
nativequery
imaplib
mencoder
dylan
ojb
google-checkout
uikit-dynamics
codeigniter-form-helper
architectural-patterns
rbm
rfc822
systrace
motorola-droid
datejs
session-hijacking
symfony-1.2
hs-err
javah
qprocess
calendarextender
zend-acl
qlibrary
moores-law

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