code-first

C# How to use DataAnnotations StringLength and SubString to remove text

坚强是说给别人听的谎言 提交于 2019-12-05 13:52:11
问题 I have a model classes that has a description property with a data annotation attribute of StringLength and length is set to 100 characters. When this property is more than 100 characters and Entity Framework tries to save this property I get the following error. [StringLength(100, ErrorMessage = "Description Max Length is 100")] public string Description { get; set; } Error: "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details" I'm not sure if

EF Code First, how can I achieve two foreign keys from one table to other table?

喜你入骨 提交于 2019-12-05 12:57:54
I've recently downloaded Entity Framework Code First CTP5 , and have a trouble with this scenario. I have two tables as follows: Members table : ID Name Comments table : ID Comment CommentedMemberID CommentMemberID And, the data should be like the following: Members ID Name 1 Mike 2 John 3 Tom Comments ID Comment CommentedMemberID CommentMemberID 1 Good 1 2 2 Good 1 3 3 Bad 2 1 Then, I coded as shown below: public class Member { public int ID {get; set; } public string Name { get; set;} public virtual ICollection<Comment> Comments { get; set;} } public class Comment { public int ID { get; set;

Code first migrations - what connection string will it use?

自闭症网瘾萝莉.ら 提交于 2019-12-05 11:51:55
问题 Code first migrations have been working very well for me. I have a services project and a wpf project. The model is in the services project which is referenced by the wpf project. Update-database is done on the services project, but uses connection string from the wpf project. I now add a web project which also references the service project. So now that there is a connection string in the app.config and there is one in the web.config, which one will it use? 回答1: In my scenario, the app

Entity Framework CTP4 Code First: Mapping protected properties

吃可爱长大的小学妹 提交于 2019-12-05 10:01:11
I would like to use a lazy-loading collection on a model, but I want Add/Remove functionality to be done through separate methods. So something like this: class Model { protected virtual ICollection<Something> _somethings { get; set; } public IEnumerable<Something> Somethings { get { return _somethings; } } public void AddSomething(Something thingToAdd) { /* logic */ _somethings.Add(thingToAdd); } } I can't figure out how to configure the mapping for this. I looked into using a configuration class: EntityConfiguration. But since the property is protected I can't figure out how to set a

Revert database in Entity Framework code-first

我是研究僧i 提交于 2019-12-05 09:47:17
I made a in my model and updated database using Add-Migration sdfgfsd Update-Database Now I found the change I just made is not necessary. I would like do revert both code and database. How can I do this? Yes - it's straightforward. Use Update-Database -TargetMigration:zzzz Where zzzz is the name of the migration before the one you want to rollback. EDIT You then need to delete the migration(s) after zzzz. If you want to rollback all migrations, or if you only have one migration, use update-database -target:0 来源: https://stackoverflow.com/questions/30506137/revert-database-in-entity-framework

Entity Framework Navigation Properties looping issue though WCF

半世苍凉 提交于 2019-12-05 08:50:35
I have a model like public class User { [Key] public long UserId { get; set; } [Required] public String Nickname { get; set; } public virtual ICollection<Group> Memberships { get; set; } } public class Group { [Key] public long GroupId { get; set; } [Required] public String Name { get; set; } public virtual ICollection<User> Members { get; set; } } public class DataContext : DbContext { public DbSet<User> Users { get; set; } public DbSet<Group> Groups { get; set; } public DataContext() { Configuration.LazyLoadingEnabled = true; } protected override void OnModelCreating(DbModelBuilder

Entity Framework 4 Code-First many to many insert

两盒软妹~` 提交于 2019-12-05 05:39:25
问题 I'm using code-first pattern for database layer. I have two POCO classes: public class Order { [Key] public int OrderId { get; set; } public virtual ICollection<Item> Items { get; set; } // other fields } and public class Item { [Key] public int ItemId { get; set; } public virtual ICollection<Order> Orders { get; set; } // other fields } Then I have data context class: public class DataContext : DbContext { public DbSet<Item> Items { get; set; } public DbSet<Order> Orders { get; set; } } And

code first membership provider

匆匆过客 提交于 2019-12-05 04:41:51
问题 How can I integrate EF 5.0 with membership provider using code first? I have my own database schema which I want to use for registration of users etc. 回答1: You should take a look at the SimpleMembershipProvider It is very easy to use it together with EF. Update For MVC4 I would start with the blank template. you need WebSecurity.InitializeDatabaseConnection to set up the database. WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "Id", "UserName", true); It takes as

Multiplicity constraint violated SQL Server 2008 - CodeFirst

喜你入骨 提交于 2019-12-05 03:01:42
I'm working to solve a very tedious problem. I have a class called Nation and a class called NationAlly public class Nation { public int ID {get; set;} public int name {get;set;} public List<NationAlly> NationAllies {get;set;} } public class NationAlly { public int ID {get; set;} public int level {get;set;} public Nation toNation {get;set;} } I'm using EF 4 and CodeFirst with a DbContext called NationsDB to manage my database on SQL Server 2008. If I create a new object of type Nation and I try to call nationsDB.SaveChanges, I got the following exception: "Multiplicity constraint violated. The

EF One-to-many Foreign Keys without child navigation properties

情到浓时终转凉″ 提交于 2019-12-05 01:08:27
Using code-first Entity Framework and .NET 4, I'm trying to create a one-to-many relationship between parents to children: public class Parent { [Key] public int ParentId { get; set; } [Required] public string ParentName { get; set; } public IEnumerable<Child> Children { get; set; } } public class Child { [Key] public int ChildId { get; set; } [ForeignKey] public int ParentId { get; set; } [Required] public string ChildName { get; set; } } As pointed out here , in order for foreign key relationship to carry into the database, the actual objects must be linked, not just their IDs. The normal