ef-code-first

Is it possible perform actions in POCO before insert/update?

夙愿已清 提交于 2020-01-03 00:24:12
问题 Let's assume this Entity Framework sample: public class User { public int UserID { get; set; } public string Name { get; set; } } public class MyDbContext : DbContext { public DbSet<User> Users { get; set; } } class Program { static void Main(string[] args) { using (var db = new MyDbContext ()) { var user = new User { Name = "Foo"}; db.Users.Add(user); db.SaveChanges(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } } I want add to User class events, like "BeforeInsert"

Is it possible perform actions in POCO before insert/update?

江枫思渺然 提交于 2020-01-03 00:24:09
问题 Let's assume this Entity Framework sample: public class User { public int UserID { get; set; } public string Name { get; set; } } public class MyDbContext : DbContext { public DbSet<User> Users { get; set; } } class Program { static void Main(string[] args) { using (var db = new MyDbContext ()) { var user = new User { Name = "Foo"}; db.Users.Add(user); db.SaveChanges(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } } I want add to User class events, like "BeforeInsert"

How to properly implement Inheritance in EF, with performance in mind

风格不统一 提交于 2020-01-02 11:20:44
问题 My app has Posts and Flags . Both of these have Responses , which have Comments . Post Response Comment Flag Response Comment I'm trying to model this in EF as simply and properly as possible, but I'm not sure if I'm going overboard here with inheritance. This is what I have: public class Post { [Key] [Required] public int PostId { get; set; } public virtual ICollection<PostResponse> Responses { get; private set; } } public class Flag { [Key] [Required] public int FlagId { get; set; } public

Data Migration In MVC3 (Using EF.4.3)

十年热恋 提交于 2020-01-02 10:30:48
问题 I am doing data-migration for my project. But I have one question, for example: I have Book table with following fields: ID Name Color 1 Java red 2 MVC blue 3 .Net blue I tried to change the name of field from "Color" to "BookColor" using Code First tech. But after migration the table looked like this: ID Name BookColor 1 Java null 2 MVC null 3 .Net null I lost my field values. How can I make sure that all value are transfered? I'm using Entity Framework with MVC3 EDIT This is my DBMigration

Entity Framework Code First initializing foreign keys

淺唱寂寞╮ 提交于 2020-01-02 09:42:10
问题 This is how the code looks like: public class Family { public int FamilyID { get; set; } public virtual ICollection<Member> FamilyMembers { get; set; } } public class Member { public int MemberID { get; set; } public virtual Family Family { get; set; } } public class Bicycle { public int BicycleID { get; set; } public virtual Member Owner { get; set; } } public class MyContext : DbContext { public MyContext : base () { } } So, there is a one-to-one relantionship between Member and Bicycle,

ASP.NET MVC3 - “Object reference not set to an instance of an object” error

帅比萌擦擦* 提交于 2020-01-02 08:44:34
问题 I'm relatively new to .NET and MVC3. I'm having some trouble with the above error message when trying to add an instance of an object. Below is my code: KeyDate class public class KeyDate { [Key] public int KeyID { get; set; } [StringLength(1000), Required] public string Title { get; set; } [Required] public string Description { get; set; } [Required] [Display(Name = "Event Date")] public DateTime EventDate { get; set; } [Display(Name = "Event End Date")] public DateTime? EventEndDate { get;

Login failed for user “xxx” Failed to open the explicitly specified database solution

允我心安 提交于 2020-01-02 07:33:12
问题 Im using entity framework code first. When starting the application the application tries to create the database on SQLServer2008R2. The error message that I'm receiving is: "Login failed for user "NT instans\Networkservice" Failed to open the explicitly specified database" After looking more carefully at the problem in the SQLServer log i can see that the error code is: 18456 severity 14 with state 38. Read about it on: http://sql-articles.com/articles/troubleshooting/troubleshooting-login

Why does EF Code First [InverseProperty] attribute fail to work when used with [ForeignKey] attribute?

自作多情 提交于 2020-01-02 07:13:26
问题 Using: EF 4.3.1, Visual Studio 2010, SQL CE 4.0 My understanding is that when declaring Foreign Keys with DataAnnotation in EF, it can be done either of the following ways: Option 1- [ForeignKey("Player1Home")] public long? HPlayer1Id { get; set; } public virtual Player Player1Home { get; set; } Option 2- public long? HPlayer1Id { get; set; } [ForeignKey("HPlayer1Id")] public virtual Player Player1Home { get; set; } Problem: When the InverseProperty DataAnnotation gets used with Option 2 an

Is that possible to have a single DbContext, with migration files being split in different assemblies?

天涯浪子 提交于 2020-01-02 07:00:10
问题 I've built a system which have a Core solution and a Custom solution for each of our clients. The Core has it own dbcontext, and each Customs have their own dbcontext for client specific tables. Custom solution is referencing Core solution dlls. Custom dlls are being dynamically loaded by the Core solution in a plugin architecture. Since Core and Custom have their own dbcontext, each one have their own migration files. All is working pretty well except for one thing, i cannot make a linq

Code first: Database only created after execute certain code?

不羁的心 提交于 2020-01-02 06:45:53
问题 I want to create a database using code first. And my database is always in "DropCreateDatabaseAlways" mode. I notice that, if I don't try to execute some query against the database, such as using (var db = new Models.TnHContext()) { var query = from u in db.Users select u; foreach (var u in query) { txt += u.UserID + "<br/>"; } } the database won't be created. Unless I execute it, only I can see the database created. Wonder if this is due to EntityFramework design/default behavior? 回答1: The