ef-code-first

Entity Framework Core Code-First: Cascade delete on a many-to-many relationship

血红的双手。 提交于 2020-01-01 08:55:53
问题 I'm working on an ASP.NET MVC 6 project with Entity-Framework Core (version "EntityFramework.Core": "7.0.0-rc1-final" ) backed by a SQL Server 2012 express DB. I need to model a many-to-many relationship between a Person entity and an Address entity. As per this guide I modeled it with a PersonAddress join-table entity, because this way I can store some extra info. My goal is to set-up my system this way: If a Person instance is deleted, all the related PersonAddress instances must be deleted

EF Code First Migrations: MigrateDatabaseToLatestVersion without NUGET

不问归期 提交于 2020-01-01 08:22:13
问题 I need help to clarify how EF Code First Migrations works on production machine. I've some entity classes and DbContext-derived class to access entities. Now, I want to perform these several things: when my application starts, it must create database, if database does not exists; then database schema must be adjusted to the model; if database was created just now, I want to create some indexes; also, if database was created just now, it must be seeded by some initial data; all of these things

Code first - how can I save an ICollection when I'm not setting the parent object to EntityState.Modified?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 06:16:27
问题 If I have the below class: public class Foo() { public int PropertyIWantUpdated {get; set;} public int PropertyIDontWantUpdated (get; set} public ICollection<Bar> Bars {get; set;} } When saving to my db, instead of context.Entry(thisFoo).State = EntityState.Modified; I'm using context.Entry(thisFood).Property(tf => tf.PropertyIWantUpdated).IsModified = true; how can I also save changes to Bars? 回答1: It depends what you are trying to update. First of all let me clarify one important fact - if

EF5 code first with ASP.NET Web API: Update entity with many-to-many relationship

穿精又带淫゛_ 提交于 2020-01-01 05:23:12
问题 I'm trying to update a Customer in my database using ASP.NET Web API and Entity Framework 5 code-first, but it's not working. My entities look like this: public class CustomerModel { public int Id { get; set; } public string Name { get; set; } // More fields public ICollection<CustomerTypeModel> CustomerTypes { get; set; } } public class CustomerTypeModel { public int Id { get; set; } public string Type { get; set; } [JsonIgnore] public ICollection<CustomerModel> Customers { get; set; } }

Create Foreign Key using Data Annotations

馋奶兔 提交于 2020-01-01 05:18:12
问题 In the code below, I need to set a foreign key constrant on ParentInfoAddProperties.ParentQuestionAnswersId so that it is dependent on ParentQuestionAnswers.Id (which is a Primary Key). I am attempting to do so using data annotations but Entity Framework 6 wants to create a new Foreign Key column in my ParentQuestionAnswers table which references the ParentInfoAddProperties.Id column not the ParentInfoAddProperties.ParentQuestionAnswersId column. I do not want Entity Framework to create a new

Entity Framework 4.1 Code First: Get all Entities with a specific base class

拜拜、爱过 提交于 2020-01-01 05:13:12
问题 I have a DbContext with set up different DbSet<T> s with all types that derive from the same base class: public class Foo : Entity { } public class Bar : Entity { } MyDbContext : DbContext { public DbSet<Foo> Foos { get; set; } public DbSet<Bar> Bars { get; set; } } Is it possible to get all entities which have the Entity base class in one query, like: DbContext.Set<Entity>(); // doesn't work I tried to introduce an explicit DbSet<Entity> to the DbContext, but that results in one big table

ASP.NET Identity - Error when changing User ID Primary Key default type from string to int AND when using custom table names

微笑、不失礼 提交于 2020-01-01 05:10:08
问题 I'm using Microsoft.AspNet.Identity 2.0.0-beta1 and Entity Framework 6.1.0-beta1 (released 11 Feb 2014). I'm getting the following error when I try to change the default type of User ID Primary Key from string to int AND when I try to use custom table names (so User.MyUsers instead of dbo.AspNetUsers): " The entity types 'IdentityUser' and 'ApplicationUser' cannot share table 'MyUsers' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship

EF Code First thinks database is out of date — only in production

和自甴很熟 提交于 2020-01-01 03:42:05
问题 I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0 and Entity Framework 4.1 Code First. It works fine locally , but in production, it fails with the following error: The model backing the 'foobar' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and

Partially updating object with EF Code First and ASP.NET MVC

这一生的挚爱 提交于 2020-01-01 00:21:13
问题 EF4.1-Code-First-Gurus! I wonder if there is a more elegant way to handle the following ASP.NET MVC 3 EF 4.1 Code First scenario: Lets say we have the following POCOs: public class Entity { [Key] public int Id { get; set; } [ScaffoldColumn(false)] public DateTime CreatedOn { get; set; } [ScaffoldColumn(false)] public DateTime ModifiedOn { get; set; } } and public class Person : Entity { public string FirstName { get; set; } public string LastName { get; set; } public DateTime Birthday { get;

Entity framework code first migrations, sql user permissions?

荒凉一梦 提交于 2019-12-31 21:22:50
问题 What is the minimal permission needed on a sql server user/login for it to be able to run entity framework code first database migrations? I naively would have thought that a user with the roles db_datareader, db_datawriter, Grant Alter on the Schema and Grant Create Table would be permissive enough. 回答1: you need following permision on the database. [db_datareader] [db_datawriter] [db_ddladmin] For full control over database use [db_owner] 回答2: Clearly it depends on what your migrations are/