ef-code-first

Entity framework code first migrations, sql user permissions?

 ̄綄美尐妖づ 提交于 2019-12-31 21:22:12
问题 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/

Get a list of elements by their ID in entity framework

陌路散爱 提交于 2019-12-31 17:51:12
问题 How can I get all elements that are in another list by ID? For eg; I have List roles; I'd like to get all roles from the database that are in this this list by their Id. I'm using code-first. I did this and it threw an error: var roles = db.Roles.Where(r => user.Roles.Any(ur => ur.RoleId == r.RoleId)); RoleId is of type int. Error: Unable to create a constant value of type 'SampleMVC.Domain.Role'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. 回答1: var

Code first - Cannot insert the value NULL into column 'Id'

萝らか妹 提交于 2019-12-31 17:50:20
问题 This is my code, very simple... var newUser = new User(); newUser.Id=id; newUser.Email = email; this.DataContext.Set<User>().Add(newUser); this.DataContext.SaveChanges(); The error I get is a sqlexception at this.DataContext.SaveChanges(); stating that: Cannot insert the value NULL into column 'Id', table 'xxxxx.dbo.Users'; column does not allow nulls. INSERT fails. I have debugged and found that there is value in Id & Email in newUser at this.DataContext.Set<User>().Add(newUser); If this is

EF 6.0 Migrations: ContextKey in MigrationHistory is null

China☆狼群 提交于 2019-12-31 17:37:28
问题 I've updated to EF6 and it hasn't been fun. I have created a new migration that is just changing two fields to nullable. public partial class AllowNullableFieldsForImage : DbMigration { public override void Up() { AlterColumn("dbo.Scabs", "image_height", c => c.Int()); AlterColumn("dbo.Scabs", "image_width", c => c.Int()); } public override void Down() { AlterColumn("dbo.Scabs", "image_width", c => c.Int(nullable: false)); AlterColumn("dbo.Scabs", "image_height", c => c.Int(nullable: false));

Entity Framework 6 code first: setting unicode to false for string properties

自古美人都是妖i 提交于 2019-12-31 10:43:04
问题 In my model i have some entities decorated with StringLength attribute: [StringLength(128)] public string FirstName { get; set; } Also i have disable unicode for all string properties this way: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Properties<string>().Configure(p => p.IsUnicode(false)); } The problem is that all string properties decorated with the mentioned attribute are ignoring this setting when generating

Entity Framework 6 code first: setting unicode to false for string properties

故事扮演 提交于 2019-12-31 10:41:57
问题 In my model i have some entities decorated with StringLength attribute: [StringLength(128)] public string FirstName { get; set; } Also i have disable unicode for all string properties this way: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Properties<string>().Configure(p => p.IsUnicode(false)); } The problem is that all string properties decorated with the mentioned attribute are ignoring this setting when generating

project.Models.tableName:The field pvid must be a string or array type with a maximum length of '20'

╄→гoц情女王★ 提交于 2019-12-31 07:14:40
问题 I encounter exception error below when i try to add a new entry in the database with : db.patient_visit.Add(pv); db.SaveChanges(); An exception of type 'System.InvalidOperationException' occurred in project.dll but was not handled in user code Additional information: project.Models.tableName:The field pvid must be a string or array type with a maximum length of '20'. Before this, my model is automatically and originally set like below because i did Code-First migration with existing database.

Seeding method is inserting additional Entities with NULL values

房东的猫 提交于 2019-12-31 05:14:54
问题 I am having this strange behavior all of a sudden (i have compared my files in version control (tfs) to be sure i did not change anything and i didn't found anything different). I am seeding my database with some metadata and i see that it has a very strange behavior i never saw before. I am inserting a Entity "Product" and it inserts this entity 2 times , first insert is correct and has everything it should have, the other one has NULL properties (string values) but some (like datetimes)

Entity Framework Code First - Cannot insert duplicate key in object 'db'

余生长醉 提交于 2019-12-31 04:53:07
问题 I'm using the new EF code first to one project of mine, and i'm getting a weird error, my mode is: abstract class Member { public virtual int MemberId; ... some other stuff } class User : Member { public virtual string Name; ... some more props no new key defined } class MyDbContext { public DbSet<User> Users {get;set;} } The result of the deployment to SQL Server is just ok, i've a Members Table and a Users Table (TPC). Now in my main i've the following code: static Main() { User x,y; using

EntityFramework CodeFirst Mapping to have camel case in the database

旧街凉风 提交于 2019-12-31 03:46:05
问题 I've just started a new company who are happy for me to use CodeFirst - but want the database fields camel cased. I'm currently writing a lot of this kind of stuff ... [Column("active")] public bool Active { get; set; } [Column("paymentType")] public string PaymentType { get; set; } Is there any way I can just set it all to Camel Case the database, rather than having to decorate all my properties? Thanks 回答1: You can use code first custom conventions If using Fluent Api, you could