I'm using EF4.1 Code First. I can't get a very simple insert to save the related entity ID. The generated SQL always inserts NULL for any related entities. Example code is below. Can anyone see what I'm doing wrong here? It does properly insert non-entity properties, such as strings. Also I have similar code in a DB initializer class to seed test data and it seems to work fine.
using (var ctx = new DataContext())
{
ctx.Users.Attach(existingUser);
// create item and add to context
var newItem = new MyItem();
ctx.MyItems.Add(newItem);
// set related entity
newItem.CreatedBy = existingUser;
ctx.SaveChanges();
}
Your code should work with default configuration of your DbContext. One possible explanation that it does not work is that you have automatic change detection disabled, for instance if you have in your context's constructor something like:
public DataContext()
{
this.Configuration.AutoDetectChangesEnabled = false;
}
In this case EF would not detect the change of the navigation property newItem.CreatedBy after you have added the new item to the context. (SaveChanges would detect this last change if change detection isn't disabled.)
You can change your code so that setting the navigation property happens before you add the new item to the context:
using (var ctx = new DataContext())
{
ctx.Users.Attach(existingUser);
// create item and add to context
var newItem = new MyItem();
// set related entity
newItem.CreatedBy = existingUser;
ctx.MyItems.Add(newItem);
ctx.SaveChanges();
}
This should work with and without automatic change detection.
Try this:
using (var ctx = new DataContext())
{
ctx.Users.Attach(existingUser);
// create item and add to context
var newItem = new MyItem();
ctx.MyItems.Add(newItem);
// set related entity
newItem.CreatedBy = existingUser;
// Added
ctx.ObjectStateManager.ChangeObjectState(newItem.CreatedBy, EntityState.Added);
ctx.SaveChanges();
}
If that doesn't work, change the line with:
ctx.ObjectStateManager.ChangeObjectState(newItem.CreatedBy, EntityState.Modified);
Added 1 line... Hope it helps.
Related
I'm working on a project that uses EFCore 2.1.0-preview1-final code first approach. Like in EF6 (and previous versions) I want to ensure the compatibility of my DbContext (and models) to the database.
In EF6 it was enabled by default and it was possible to deactivate it with Database.CompatibleWithModel(false);. As far as I know EF uses the __MigrationHistory table where the model information was stored. EFCore has no such column in __EFMigrationsHistory table that could provide such information.
I cannot find any information about compatibility check in EFCore. But I want to ensure the compatibility, because after some tests it seems not to be enabled by default (or does exist). I tested it by adding and deleting some columns from database manually and executing the application after the modifications. I - against my expectation - received no exception.
Does anybody know how to achieve a compatibility check from model to database and vice versa like in EF6 for EFCore?
Or could provide some helpful links for further information about it or why it doesn't exist in EFCore (because it is not necessary)?
I strongly advise against doing this since it uses internal components and is error-prone, but here's one way to do it.
using (var db = new MyDbContext())
{
var reporter = new OperationReporter(handler: null);
var designTimeServiceCollection = new ServiceCollection()
.AddSingleton<IOperationReporter>(reporter)
.AddScaffolding(reporter);
new SqlServerDesignTimeServices().ConfigureDesignTimeServices(designTimeServiceCollection);
var designTimeServices = designTimeServiceCollection.BuildServiceProvider();
var databaseModelFactory = designTimeServices.GetService<IScaffoldingModelFactory>();
var databaseModel = (Model)databaseModelFactory.Create(
db.Database.GetDbConnection().ConnectionString,
tables: new string[0],
schemas: new string[0],
useDatabaseNames: false);
var currentModel = db.Model;
// Fix up the database model. It was never intended to be used like this. ;-)
foreach (var entityType in databaseModel.GetEntityTypes())
{
if (entityType.Relational().Schema == databaseModel.Relational().DefaultSchema)
{
entityType.Relational().Schema = null;
}
}
databaseModel.Relational().DefaultSchema = null;
databaseModel.SqlServer().ValueGenerationStrategy =
currentModel.SqlServer().ValueGenerationStrategy;
// TODO: ...more fix up as needed
var differ = db.GetService<IMigrationsModelDiffer>();
if (differ.HasDifferences(databaseModel, currentModel))
{
throw new Exception("The database and model are out-of-sync!");
}
}
I've looked at so many posts about this, but still haven't found the solution:
I'm using a winforms app that uses EntityFramework (6?). When I load the form I can read from the DB using the context (Entities). However, when I savechanges after adding a new entity, it doesn't persist to the db.
var c = new Card { Name = tbName.Text, Quantity = int.Parse(tbQuantity.Text) };
dbContext.Cards.Add(c);
dbContext.SaveChanges();
The dbContext is setup in the form constructor and is an instance of "LiquorTrackEntities".
LiquorTrackEntities dbContext;
public Form1()
{
InitializeComponent();
dbContext = new LiquorTrackEntities()
Reading from the db works:
var cards = dbContext.Cards.ToList();
I do this stuff all the time in asp.net MVC, but it isn't working in WinForms.. is there something special I have to do in winforms? I also know about the normal "using (var db = new LiquorEntitiesEntities())" convention, but I just want to get this functioning before I worry about convention.
Any ideas?
Just tried this to no avail:
var c = new Card { Name = tbName.Text, Quantity = int.Parse(tbQuantity.Text) };
dbContext.Cards.Attach(c);
dbContext.Entry(c).State = EntityState.Added;
dbContext.SaveChanges();
Just tried creating a new EDMX using EF5 instead.. same problem.
UPDATE:
SaveChanges does return a 1 when after adding a card. It stays in the context (if I reload my cards from the context, the new one is there..) but never makes it to the database.
Good day, stackoverflow.
My question is: when the entity from DataContextModel is instantiated somewhere in the code, does it already have references to the database record? Or can it be used as common class ? For example:
public void SomeMethod()
{
var FirstEntity = new DBEntity(); //DBEntity is some entity from the database
var SecondEntity = new DBEntity();
var ThirdEntity = new DBEntity();
DbSet<DBEntity>.Add(SecondEntity);
DbSet<DBEntity>.Add(ThirdEntity);
DbContext.SaveChanges();
}
So, will FirstEntity be affected somehow, or it will be removed, when the SomeMethod exits?
Thanks in advance.
It's just a plain class if outside the context.
But be careful, suppose this FirstEntity is referenced inside another object, as in:
var FirstEntity = new DBEntity();
var SecondEntity = new DBEntity();
var ThirdEntity = new DBEntity();
DbSet<DBEntity>.Add(SecondEntity);
DbSet<DBEntity>.Add(ThirdEntity);
SecondEntity.Sibling = FirstEntity;
When you save changes, if Sibling in the example is a foreign key reference, it will automatically add FirstEntity to the database.
Another example to be clear:
var Computer = new ComputerEntity();
var Motherboard = new MotherboardEntity();
Computer.Motherboard = Motherboard;
DbSet<ComputerEntity>.Add(Computer);
DbContext.SaveChanges();
This will save both Computer and Motherboard to the DB.
Im wanting to use Entity Framework POCO in a disconnected (from context) mode. In my scenario I'm creating a new Parent object and want to attach an existing child object to it and then save it to the db.
The code below undesirably inserts a new Course record when saving a new Student record, when instead I want the existing Course record linked to the new Student record.
How can I do this in Entity Framework where...
the objects can be disconnected from the context. (i.e. Queried in one context and then saved in a different context)
I dont need to re-query the child record from the DB just so I can attach it to the parent when I'm saving to db. I really want to avoid doing extra trips to the db when I already have it as an object in memory.
This page shows a database diagram that the code below is based on http://entityframeworktutorial.net/EF4_EnvSetup.aspx#.UPMZ4m-UN9Y
class Program
{
static void Main(string[] args)
{
//get existing course from db as disconnected object
var course = Program.getCourse();
//create new student
var stud = new Student();
stud.StudentName = "bob";
//assign existing course to the student
stud.Courses.Add(course);
//save student to db
using (SchoolDBEntities ctx = new SchoolDBEntities())
{
ctx.Students.AddObject(stud);
ctx.SaveChanges();
}
}
static Course getCourse()
{
Course returnCourse = null;
using (var ctx = new SchoolDBEntities())
{
ctx.ContextOptions.LazyLoadingEnabled = false;
returnCourse = (from s in ctx.Courses
select s).SingleOrDefault();
}
return returnCourse;
}
}
I believe there are few ways of accomplishing this.
You can specify that course entity is unchanged rather than added, along these lines:
ctx.Entry(course).State = EntityState.Unchanged;
Or instruct your context, that you are working with existing entity:
ctx.Courses.Attach(course);
More info here:
http://msdn.microsoft.com/en-us/data/jj592676.aspx
EDIT
There are some running samples from my solution, I verified they work as expected.
In all cases we have Publisher record in database with ID = 2 and Name = "Addison Wesley" (irrelevant to the example, but just for good measure).
Approach 1 - Setting Entity State
using (var context = new Context())
{
var book = new Book();
book.Name = "Service Design Patterns";
book.Publisher = new Publisher() {Id = 2 }; // Only ID is required
context.Entry(book.Publisher).State = EntityState.Unchanged;
context.Books.Add(book);
context.SaveChanges();
}
Approach 2 - Using Attach method
using (var context = new Context())
{
var book = new Book();
book.Name = "Service Design Patterns";
book.Publisher = new Publisher() { Id = 2 }; // Only ID is required
context.Publishers.Attach(book.Publisher);
context.Books.Add(book);
context.SaveChanges();
}
Approach 3 - Setting Foreign Key value
using (var context = new Context())
{
var book = new Book();
book.Name = "Service Design Patterns";
book.PublisherId = 2;
context.Books.Add(book);
context.SaveChanges();
}
For this last approach to work I needed to add extra property PublisherId, it has to be named according to NavigationPropertyName + 'Id" convention to be picked up by EF auotmatically:
public int PublisherId { get; set; }
public Publisher Publisher { get; set; }
I am using here EF5 Code First, but it is very similar to POCO.
Entity Framework does not allow relationships that cross contexts.
If you place the reading of the course and connecting the course to the student within the same using statement, it would work.
I also tried the second option it worked for me. I did like the parent->child relationship happening at an object level first and save to db. Maybe I should just remove all the relationships between the entities that EF generates and manually control this myself.
I do :
var t = cboTrailer.SelectedItem as Trailer;
using (var db = new CAPSContainer())
{
db.Attach(t);
db.Trailers.DeleteObject(t);
db.SaveChanges();
}
This deletes a trailer object from the context and pushes those changes. But it fails and I get :
Entities in 'CAPSContainer.Trailers' participate in the 'CustomerTrailer' relationship. 0 related 'Customer' were found. 1 'Customer' is expected.
The issue is that the relationship between Customers and Trailers is like ---> Customer 1-* Trailer ... So if I delete the trailer it shouldnt be an issue.
So why the error?
EDIT:
I tried with both v4 and 4.4 dlls of EF. It seems if I attach the Customer first it all works, but closer inspection shows that even though no error occurs the trailer still remains.
var c = cboCustomer.SelectedItem as Customer;
var t = cboTrailer.SelectedItem as Trailer;
using (var db = new CAPSContainer())
{
db.Attach(c);
c.Trailers.Remove(t);
db.SaveChanges();
}
PopulateTrailers();
--> still shows in list + even after app restart its still there...
EDIT 2:
This almost works :
using (var db = new CAPSContainer())
{
db.Attach(c);
db.Attach(t);
c.Trailers.Remove(t);
db.Trailers.DeleteObject(t);
db.SaveChanges();
}
It gives (on 2nd delete) :
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
Its crazy that in the same project just before when I was using a single object context I did ..
MyContext.DeleteObject(t) and that was it. Now that same line is replaced with 5 lines and still there is an error.
You may need to attach the customer that relates to the trailer you are deleting so that EF can remove the relationship as well.
Edit
In addition to removing the trailer from the Customer, you need to delete it from the context as well. Removing it from the customer only removes the relationship.
var c = cboCustomer.SelectedItem as Customer;
var t = cboTrailer.SelectedItem as Trailer;
using (var db = new CAPSContainer())
{
db.Attach(c);
c.Trailers.Remove(t);
db.Trailers.DeleteObject(t);
db.SaveChanges();
}
PopulateTrailers();
When you load the source elements of the cboCustomer, do you include the Trailer?
like:
cboCustomer.Items = db.Customers.Include("Trailers").ToList();
and then delete the elements with:
using (var db = new CAPSContainer())
{
db.Attach(c);
c.Trailers.Remove(t);
db.SaveChanges();
}