ef-code-first

MySQL + Code First + Lazy Load problem !

别等时光非礼了梦想. 提交于 2019-12-23 15:15:19
问题 in a very simple real estate program I'm trying to list all images for a house using EF Code First attached to an existing DB I have, I'm using MySQL Conector 6.3.6, here's my code. namespace CodeFirstMySQL { class Program { static void Main(string[] args) { RealEstate db = new RealEstate(); var houses = (from h in db.Houses select h).Take(10); foreach (var house in houses) { Console.WriteLine(string.Format("Images for {0}", house.Address)); foreach (Image image in house.Images) { Console

EF Code first : Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF

坚强是说给别人听的谎言 提交于 2019-12-23 12:47:32
问题 Set the primary key to identity in database (SQL Server) When using EF to insert data I am getting error Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF with this particular entity, though I am using the same approach with other entities to insert the data and getting no error with them. public class MyTable { [Key/*,DatabaseGenerated(DatabaseGeneratedOption.Identity)*/] public int ID{get;set;} public string name {get;set;} public string

Adding a migrations gives filename already exists

微笑、不失礼 提交于 2019-12-23 12:39:15
问题 I have enabled migrations as such: enable-Migrations -ProjectName ProjectOne -ContextTypeName MyIdentity.Config.MyIdentityContext -MigrationsDirectory Identity\\Migrations I specified my context as it is in a separated namespace, and i specified the directory because i want to have the migrations in a different directory. After enabling the migrations like this, i get the expected configuration file in the expected location (Identity\Migrations folder) (i removed the comments in the seed

Unable to Determine Principal End - Entity Framework Code First Relationship

谁说胖子不能爱 提交于 2019-12-23 10:59:20
问题 PLEASE CHECK POSSIBLE SOLUTION SECTION BELOW I have an issue with a foreign key relationship - here are my tables: public class Lead { [Key] public int LeadId { get; set; } public int? CustomerId { get; set; } [ForeignKey("CustomerId")] public virtual Customer Customer { get; set; } } public class Customer { [Key] [Column("customer_id")] public int CustomerId { get; set; } [ForeignKey("CustomerId")] public virtual Lead Lead { get; set; } } I'm having an issue where I receive this error:

Entity Framework Code First Multi Column Foreign Key

蹲街弑〆低调 提交于 2019-12-23 10:48:32
问题 Using code first I designed 3 classes: class User { public Int32 ID {get;set;} public virtual ICollection<UserCityDetail> {get;set;} public Int32 MainCityID {get;set;} public UserCityDetail MainCityDetail {get;set;} } class City{ public Int32 ID {get;set;} ... } class UserCityDetail{ [Key, Column(Order = 0)] public Int32 UserID {get;set;} [Key, Column(Order = 1)] public Int32 CityID{get;set;} ... } So basically I have a user that have different details on several cities. User ID is both PK

How to specify a schema for a many-to-many relationship table?

与世无争的帅哥 提交于 2019-12-23 09:30:05
问题 I have a User-Role many-to-many relationship, specified by this excerpt from an EntityTypeConfiguration<Role> derived class that allows me to specifiy schemas for single tables, e.g: [Schema(SchemaNames.ParkPay)] class WeekDayConfig : EntityTypeConfigurationWithSchema<WeekDay> { internal WeekDayConfig() { Ignore(t => t.IsDeleted); Property(p => p.Name) .IsRequired() .HasMaxLength(20); } } Now, for Role , the configuration class contains this code, and the resultant table UserRole gets created

A column ID occurred more than once in the specification?

扶醉桌前 提交于 2019-12-23 09:14:40
问题 I am getting this error message when using EF 4.1 code first approach with Sql compact 4. I don't see any model who has id column more than one so i have no idea why this error occured. What can cause this error ? Edit : I want to specifiy few extra things. Database creating is success but model creating is not. And Exception has been thrown from sqlce methods. 回答1: This issue stems from a SQL query that returns two or more columns that have an identical name. SQL will handle exact duplicate

Code first causing required relation to be optional?

ぐ巨炮叔叔 提交于 2019-12-23 09:01:05
问题 public class Client { public Int32 ClientID { get; set; } public virtual ICollection<Inquiry> InquiryManufacturers { get; set; } public virtual ICollection<Product> Products { get; set; } public virtual ICollection<Inquiry> InquiryRetailers { get; set; } } public class Product { public Int32 ProductID { get; set; } public Int32 ClientID { get; set; } public virtual Client Client { get; set; } public virtual ICollection<Inquiry> Inquiries { get; set; } } public class Inquiry { public Int32

Complex type: multiple instances in one model?

你离开我真会死。 提交于 2019-12-23 08:49:51
问题 Is there a way to have multiple instances of complex type inside the same model using Fluent api model builder? public class Contact { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Address PersonalAddress { get; set; } public Address BusinessAddress { get; set; } } public class Address { public string Street{ get; set; } public string City{ get; set; } public string PostalCode{ get; set; } } protected override void

EntityFramework anonymous composite key property name conflict

天涯浪子 提交于 2019-12-23 08:49:14
问题 I'm using EntityFramework 5 (or 4.3 for .Net Framework 4.0) In my DbContext object I have already set the correct DbSets and the objects contain proper references to each other. This is not new for me, and things are working well. Now in this case I have some composite keys that, sometimes, include the foreign key of a table (or object in this case). For this I use the HasKey<>() function on the OnModelCreating method of the DbContext. When these properties have different names, there is no