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

ILMerge.Merge: ERROR
Draw text centered in a ellipse/circle in winforms application
c# android bluetooth get socket from connected device
How to make a field auto populate in C# Visual Studio?
C# destructors and GC not really solving problems compared to C++ destructors
How to count occurences of a char in a big (5+ GBs) file using C#?
Confused about backgroundworker not stopping when expected
Ignore paging limit for Azure Mobile Service in a Universal app (UWP)
How to add object to list item in c#
Handling .NET events in COM / VBA
Reading specific XML parameter
c# charting - access from other thread exception
Using Bus.Send() and using API services inside Saga Handler
How do you place Banner Ads or Interstitial Ads in Windows Forms
C# Create a Rectangle class that holds width and height.
In c# i am always getting Autoincrementseed value as 0 in table definition even though that table's column's value set seed as 1 in ms sql

Categories

HOME
stata
ide
locale
casting
window.open
python-requests
gap-system
magento-1.9.2.1
download
jquery-select2
sitemap
samsung-gear-s2
revit-api
dcos
mongoimport
angular-universal
kairosdb
saucelabs
eclipse-orion
popup
sha1
game-engine
versioning
internet-explorer-7
android-actionbar
jq
android-wifi
ajaxcontroltoolkit
android-navigation-drawer
lotusscript
multipart
rndis
klipfolio
hololens
android-wear-2.0
ckeditor4.x
rancher
android-6.0-marshmallow
pypy
jbossfuse
xlib
overflow
opencsv
oracle-nosql
javax
minitest
asp.net-webhooks
jquery-inputmask
observer-pattern
rbac
autodesk-data-management
paperclip
synchronized
text-formatting
jcs
bioperl
ajv
richtext
selection-sort
androiddesignsupport
cc
totalview
jtextarea
acceleo
alarmmanager
cedar-bdd
eclipse-mars
git-cherry-pick
3d-rendering
cexception
video-embedding
apache2-module
angular-promise
windows-kernel
libgcrypt
polarion
jeet-grid
xmla
jsonschema2pojo
system.net
parsefloat
windows-search
pnunit
switchpreference
amzi-prolog
winrun4j
vbaccelerator
ie-developer-tools
playing-cards
google-checkout
latex-suite
exi
wordbreaker
windows-scheduler
antixsslibrary
derived-class
helicontech
fileoutputstream
datejs
session-hijacking
execjs
overlays
llblgen
onunload
versions
rpxnow
zend-acl
opengl-to-opengles
vista64

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