ef-code-first

One to Many with a join table and an optional relationship in Entity Framework 4.1 Fluent API

雨燕双飞 提交于 2019-12-07 04:56:27
问题 Again with a legacy database that cannot be changed and using Entity Framework 4.1 with the Fluent API to read data only. public class Client { [Key] public int ClientID { get; set; } public string Name { get; set ;} public virtual ICollection<Phone> Phones { get; set; } } public class Phone { [Key] public int PhoneID { get; set; } public string Number { get; set; } public virtual Client Client { get; set; } } public class ClientPhone { [Key] [Column(Order=0)] public int ClientID { get; set;

TPC Inheritance Error

浪尽此生 提交于 2019-12-07 03:39:56
问题 I've got an strange problem with TPC inheritance using C# Entity Framework Codefirst and Fluent Api. I have 3 Classes named Person , Invoice and PeriodicInvoice as you can see below. Here is a summary of my code: Invoice class and its configuration class: public class Invoice : InvoiceBase { public Person User { get; set; } } public class InvoiceConfig : EntityTypeConfiguration<Invoice> { public InvoiceConfig() { this.Map(m => { m.MapInheritedProperties(); m.ToTable("Invoices"); }); } }

Moving Entity framework to another project from MVC causes re-migration

爷,独闯天下 提交于 2019-12-07 03:27:33
问题 I currently have an asp.net MVC 4 application which contains Entity framework 6 Code First models, DbContext and Migrations. In an attempt to separate this from my web application so I can re-use these database classes in another project I have moved all related Entity Framework classes to their own project. However now when I run the solution it thinks my model has changed and attempts to run all my migrations once more. The problem appears to be in my use of SetInitializer as if I comment

“EntityType has no key defined” exception although key is defined with HasKey

痞子三分冷 提交于 2019-12-07 02:48:37
问题 Using EF 5 (reverse engineered code first), my model was working fine until it suddenly stopped. \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'ProjectsDate' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'ProjectsRisk' has no key defined. Define the key for this EntityType. I define a key using fluent API rather than attributes, here is my ProjectsDates classes. public partial class ProjectsDate { public string

Entity Framework doesn't query derived classes - Error in DbOfTypeExpression

痞子三分冷 提交于 2019-12-07 02:34:46
问题 I have a base class and two derived classes. Each of the derived classes implements the same type as a property - the only difference is the property name. Sadly I don't have much influence on the class design -> they have been generated from a wsdl file. I then have a property on the BaseType to encapsulate the common property. The plan was to use this property in my web views etc. I have used the famous "Fruit-Example" to demonstrate the problem: public class FruitBase { public virtual int

Hold Old and New value in SaveChange as DbEntityEntry.Entity to Audit

那年仲夏 提交于 2019-12-07 02:32:27
As you know we can Audit object in SaveChange() but I have some problem with Modified and Deleted entities as following, First I am using Audit.Net (this is very simple to use) to Audit my objects the below method should do audit as: private int SaveAuditRecord(DbEntityEntry dbEntry, string userGUID) { Logging.Log(LoggingMode.Error, "Saving Audid Entry{0}....", dbEntry.ToString()); DateTime changeTime = DateTime.UtcNow; TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault() as TableAttribute; string tableName = tableAttr != null

EF code first: inherited dbcontext creates two databases

主宰稳场 提交于 2019-12-07 01:24:49
问题 I'm trying to create a base dbcontext that contains all the common entities that will always be reused in multiple projects, like pages, users, roles, navigation etc. In doing so I have a ContextBase class that inherits DbContext and defines all the DbSets that I want. Then I have a Context class that inherits ContextBase where I define project specific DbSets. The classes are defined as follows: public class ContextBase : DbContext { public virtual DbSet<User> Users { get; set; } //more sets

literal or constant as part of composite key in EF code first

此生再无相见时 提交于 2019-12-07 01:24:28
问题 I am relatively new to the Code First approach to Entity Framework. I have used the Database First approach for a while now, but the Code First seems to be a better fit for the application I am currently developing. I am working with an existing MS SQL database, and I am not allowed to make any changes whatsoever to the database. The reason why I am using Code First is because the Fluent API allows me to dynamically assign a table name to a class. That said, I have a predicament where I need

ASP.NET-Identity limit UserName length

南楼画角 提交于 2019-12-07 01:06:05
问题 How can I limit the UserName field in the table AspNetUsers ? Neither this: public class ApplicationUser : IdentityUser { [Required, MaxLength(15)] public string UserName { get; set; } } or this: modelBuilder.Entity<ApplicationUser>().Property(x => x.UserName).HasMaxLength(15); works. I need this because setting an Index on an nvarchar(max) gives me this error msg: Column 'UserName' in table 'dbo.AspNetUsers' is of a type that is invalid for use as a key column in an index. To be verbose, I

MVC 3 & ASP.Net with Entity Framework scaffolding error : No model classes are available

廉价感情. 提交于 2019-12-07 01:03:11
问题 I am trying to use MVC3 with the Entity framework with Code First, but the code Template generation process in MS Visual Web Developer 2010 - is not detecting my Model in the models folder - giving a message - "No Model classes are available" - although I do have model classes for which I want to create controllers. The templating option I've selected is "Controller with read/write actions and views using the Entity Framework". I've uninstalled and reinstalled Web Developer 2010, but still