ef-code-first

Mapping SQL view in code-first approach

你离开我真会死。 提交于 2020-01-10 01:04:08
问题 I have a SQL view: WITH DirectReports (ID,ParentFolderID, ParentFolderName,FolderID,FolderName,OwnerOCID,OwnerArName,OwnerEnName,FolderType,LEVEL) AS ( SELECT e.Id AS ID,cast(cast(0 AS binary) AS uniqueidentifier) AS ParentFolderID, cast('MainFolder - ' + MainFolders.enName AS nvarchar(250)) AS ParentFolderName, e.Id AS FolderID, e.Name AS FolderName, WorkSpaces.Owner_Id AS OwnerOCID, OrgCharts.arName AS OwnerArName, OrgCharts.enName AS OwnerEnName, MainFolders.Type AS FolderType, 0 AS LEVEL

Eager loading property of derived class using Include

我是研究僧i 提交于 2020-01-09 10:03:31
问题 I have classes like: Person { Name Address } Employee : Person { Compensation - object } Visitor : Person { } If I write linq: var persons = Context.Persons .Include("Compensation"); I get error: A specified Include path is not valid. The EntityType 'Person' does not declare a navigation property with the name 'Compensation'. It works ok if I do: var persons = Context.Persons .OfType<Employee>() .Include("Compensation"); But I would like to get Employees and visitors in the same query. Looks

Eager loading property of derived class using Include

北慕城南 提交于 2020-01-09 10:03:13
问题 I have classes like: Person { Name Address } Employee : Person { Compensation - object } Visitor : Person { } If I write linq: var persons = Context.Persons .Include("Compensation"); I get error: A specified Include path is not valid. The EntityType 'Person' does not declare a navigation property with the name 'Compensation'. It works ok if I do: var persons = Context.Persons .OfType<Employee>() .Include("Compensation"); But I would like to get Employees and visitors in the same query. Looks

Entity Framework Database.SetInitializer simply not working

帅比萌擦擦* 提交于 2020-01-09 03:33:04
问题 I am having this kind of "mysterious" issue here. I am currently using Entity Framework 4.1 Code First approach with my ASP.NET MVC 3 application, it worked great, until yesterday... Something really bad happened that caused my Database.SetInitializer to stop working. Explained: I have this simple model public class User { public int Id { get; set; } public int RoleId { get; set; } [Required] [StringLength(50)] [DataType(DataType.Text)] public string Login { get; set; } [Required]

Objects in ICollection not passed with model to view

空扰寡人 提交于 2020-01-07 05:52:10
问题 I'm trying to create a site with news that can have comments. I've been following guides from pluralsight.com and I've followed what is done in the guide to the letter but when i debug and look at what's inside the model at runtime the comments aren't included. My DB class: public class ProjectDB { public DbSet<ContentNode> ContentNodes{ get; set; } public DbSet<Comment> Comments { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating

How do I get a property's original value while validating, in EF Core?

纵饮孤独 提交于 2020-01-07 05:16:06
问题 In EF Core, there is a neat way to perform entity validation using IValidatableObject . During validation I have the current value, but I also need access to the original value. This is the validation method: public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { } Is there some way to get access to the object's original values as well? I think the key is in ValidationContext but I'm not sure how. 来源: https://stackoverflow.com/questions/41771376/how-do-i-get-a

Entity Framework 6: one-to-one relationship with inheritance

喜夏-厌秋 提交于 2020-01-06 20:34:50
问题 I'm using EF6 Code First and here's a simple model which reproduces my issue: abstract class GeoInfo { public int Id { get; set; } public double CoordX { get; set; } public double CoordY { get; set; } } class PersonGeoInfo : GeoInfo { [Required] public Person Person { get; set; } } class CarGeoInfo : GeoInfo { [Required] public Car Car { get; set; } } class Person { public int Id { get; set; } public string Name { get; set; } public virtual PersonGeoInfo PersonGeoInfo { get; set; } } class

Double foreign key generation

喜你入骨 提交于 2020-01-06 19:40:28
问题 This is a followup-question of this question, where i had a similar problem. But this is solved now by default foreign key convention. My problem now is (in short), that my migrations generates int ReferencedEntityID; int ReferencedEntity_ReferencedEntityID; where one is an integer property in my model and the other one is a virtual property. My migrations generates this: "dbo.Contracts", c => new { ContractId = c.Int(nullable: false, identity: true), PricePerUnit = c.Double(nullable: false),

Double foreign key generation

二次信任 提交于 2020-01-06 19:40:07
问题 This is a followup-question of this question, where i had a similar problem. But this is solved now by default foreign key convention. My problem now is (in short), that my migrations generates int ReferencedEntityID; int ReferencedEntity_ReferencedEntityID; where one is an integer property in my model and the other one is a virtual property. My migrations generates this: "dbo.Contracts", c => new { ContractId = c.Int(nullable: false, identity: true), PricePerUnit = c.Double(nullable: false),

Do I need to create a DBSet for every table? So that I can persist child entities?

笑着哭i 提交于 2020-01-06 14:15:33
问题 My EF5 model public Layout() { public int Id {get;set} public BindingList<Column> Columns {get;set} } public Column() { public int Id {get;set} public string Name {get;set} [Required] [ForeignKey("LayoutId") public virtual Layout Layout {get;set} public int LayoutId {get;set} } In my context I only need to specify DBSet<Layout> Layouts {get;set} I dont need to specify DBSet Columns for the database to create the way I want it with both tables. Also I am happy in my code to only ever access