ef-fluent-api

How to specify a multi-column UNIQUE constraint in code-first Entity Framework fluent API

青春壹個敷衍的年華 提交于 2020-01-15 08:08:05
问题 Suppose I have the following entities: public class Calendar{ public int ID{get;set;} public ICollection<Day> Days { get; set; } } public class Day{ public int ID{get;set;} public DateTime Date{get;set;} public int CalendarID{get;set;} } This is a one-to-many relationship with CalendarID as a foreign key. The issue is I also want to make sure that each date exists only once in each calendar. That is, no two days have both the same Date and same CalendarID . In raw SQL I can do this by: ALTER

How to handle many to many same table (User) in ASP.Net MVC 5 - Fluent API

荒凉一梦 提交于 2020-01-11 12:02:33
问题 I am learning ASP.Net MVC 5 and I am stuck with one basic DB Design. So, I have one User which can refer many person in job Also, many person can apply to get themselves Referred. I have created two roles and all of that is taken care. Now, I have on class called Referral which will keep track of every instance of Referral that needs to be done. Referral Model: Public class Referral { [Key] public int ReferralId { get; set; } public Company Company { get; set; } public int CompanyId { get;

How to handle many to many same table (User) in ASP.Net MVC 5 - Fluent API

流过昼夜 提交于 2020-01-11 11:59:17
问题 I am learning ASP.Net MVC 5 and I am stuck with one basic DB Design. So, I have one User which can refer many person in job Also, many person can apply to get themselves Referred. I have created two roles and all of that is taken care. Now, I have on class called Referral which will keep track of every instance of Referral that needs to be done. Referral Model: Public class Referral { [Key] public int ReferralId { get; set; } public Company Company { get; set; } public int CompanyId { get;

One or Zero to One Entity Framework Code First FluentApi

梦想的初衷 提交于 2020-01-11 02:08:53
问题 I need to create fluentapi one or zero to one reference and have navigation properties on both of entities. EntityTwo should contain simple proerty to store foreign key (EntityOneId) public class EntityOne { public int Id { get; set; } public EntityTwo EntityTwo { get; set; } } public class EntityTwo { public int Id { get; set; } public int EntityOneId { get; set; } public EntityOne EntityOne { get; set; } } public class MyDbContext : DbContext { protected override void 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

Manually map POCO to existing Table using Entity Framework

馋奶兔 提交于 2020-01-06 01:53:07
问题 I have an existing database table Movie . Now I want to write a POCO Movie.cs which will map to that table. I want to have control over the mapping partially because I'm trying to learn more about EF. The Movie table has 3 fields: Title VARCHAR(255,), ReleaseDate DATETIME, Price MONEY . What would my DbContext - derived class look like for this? Coming from a Java/Hibernate background, I figured I would just map fields in the POCO to cols in the table, but it doesn't seem that straight

When declaring relationship is not necessary in Entity Framework 6?

喜欢而已 提交于 2020-01-05 08:08:10
问题 In EF6 we have two ways to declare relationship between two tables: Annotations Attributes Fluent API Today (by accident) I removed one relationship between two tables and everything kept working well. I was very surprised because there was no way EF would know how those two tables are connected. Tables look like that: [Table("item")] public class Item { public Item() { ItemNotes = new HashSet<ItemNote>(); } [Key] [Column("itemID", TypeName = "int")] [DatabaseGenerated(DatabaseGeneratedOption

EF 6 Codefirst -Setting default value for a property defined in base class using fluent API

两盒软妹~` 提交于 2020-01-04 13:36:17
问题 I have a base class which has audit properties like public abstract class BaseModel { [Column(Order = 1)] public long Id { get; set; } public long CreatedBy { get; set; } public DateTime CreatedDate { get; set; } public long ModifiedBy { get; set; } public DateTime ModifiedDate { get; set; } public bool IsActive { get; set; } } All my poco classes derive from this class. I am trying to set a default value to the IsActive properties. I am not keen on using annotations and hence was wandering

Configuring many One-To-One relationships with Entity Framework Code First and Fluent API

送分小仙女□ 提交于 2020-01-04 05:28:08
问题 I have a horrible database model (in MySQL though I don't think that matters) that someone else designed so I'm stuck with it, but want to use with Entity Framework with it anyway: Database Item ASSET_ID* ASSET_NAME FIELDS_ID 1 Cat 1 2 Dog 2 3 Fish 3 ItemFields ID* CUSTOMFIELD1ID CUSTOMFIELD2ID CUSTOMFIELD3ID 1 1001 1002 1003 2 1004 1005 1006 3 1007 1008 1009 Field ID* STRINGVAL 1001 Meow Mix 1002 House 1003 5lbs 1004 Kibble 1005 Yard 1006 30lbs 1007 Fish Food 1008 Tank 1009 0.5 ounces

Foreign key with constant in Fluent API

非 Y 不嫁゛ 提交于 2020-01-02 06:17:33
问题 I need to load a reqired:many relationship from database. Now my problem is, that the the key of the related table consists of three keys: public partial class EnumValue { [Key] [Column(Order = 0)] [StringLength(14)] public string EnumGroup { get; set; } [Key] [Column(Order = 1)] public byte EnumId { get; set; } [Key] [Column(Order = 2)] [StringLength(3)] public string Language { get; set; } [StringLength(50)] public string Description { get; set; } } In my other object I have only one