ef-fluent-api

One to one relationship with Entity Framework Fluent API

≯℡__Kan透↙ 提交于 2019-11-28 00:36:41
问题 I'm having trouble with reverse navigation on one of my entities. I have the following two objects: public class Candidate { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long CandidateId { get; set; } .... // Reverse navigation public virtual CandidateData Data { get; set; } ... // Foreign keys .... } public class CandidateData { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long CandidateDataId { get; set; } [Required] public long CandidateId { get;

Cascade delete using Fluent API

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:24:59
I have two entities. Profile and ProfileImages . After fetching a Profile I want to delete ProfileImages through Profile without it just removing the reference to Profile (setting it to null ). How can this be done with fluent API and Cascading Delete? Do I set the HasRequired attribute or the CascadeDelete attribute? public class Profile { //other code here for entity public virtual ICollection<ProfileImage> ProfileImages { get; set; } } public class ProfileImage { // other code here left out [Index] public string ProfileRefId { get; set; } [ForeignKey("ProfileRefId")] public virtual Profile

Map System.Uri using Entity Framework Fluent Api

a 夏天 提交于 2019-11-27 23:38:52
问题 Pretty simple question. I have a model that has a property which is a System.Uri type. Uri s don't have a default parameterless constructor, and no ID field. Is there any way to override my model generation to store it in the DB in a custom way (e.g. just as a string )? In NHibernate, I've done this before by implementing IUserType , but I could not find yet a similar mechanism in CodeFirst. Obviously, I could create a custom type that uses a Uri under the hood and exposes regular mappable

How to define Many-to-Many relationship through Fluent API Entity Framework?

无人久伴 提交于 2019-11-27 17:18:29
Below is my model: public class TMUrl { //many other properties //only property with type Keyword public List<Keyword> Keywords{get;set;} } public class Keyword { //many other properties //only property with type TMUrl public List<TMUrl> Urls{get;set;} } So clearly, both the entities have many-to-many relationship. I chose fluent api to tell the entity-framework about this relationship i.e. modelBuilder.Entity<TMUrl> .HasMany(s => s.Keywords) .WithMany(s => s.URLs).Map(s => { s.MapLeftKey("KeywordId"); s.MapRightKey("UrlId"); s.ToTable("KeywordUrlMapping"); }); but when I do url.Keywords.Add

Entity Framework Code first: cycles or multiple cascade paths

混江龙づ霸主 提交于 2019-11-27 09:09:45
I have a Booking class that has a booking contact (a Person ) and a set of navigation properties ( People ) that links through a join table to another set of navigation properties ( Bookings ) in Person . How do I generate the Booking table with cascading deletes enabled for the booking contact relationship? When I leave it out of the fluent API code (default setting of cascade delete enabled) I get the following error message from migration: Introducing FOREIGN KEY constraint 'FK_dbo.BookingPeople_dbo.People_PersonID' on table 'BookingPeople' may cause cycles or multiple cascade paths.

One to One Relationship with Different Primary Key in EF 6.1 Code First

核能气质少年 提交于 2019-11-27 07:15:55
问题 I am having an issue getting a reference to the employee object from the PayGroup object using Entity Framework 6.1. I have a foreign key in the database on PayGroup.SupervisorId -> Employee.EmployeeId. Note that this is a zero or one-to-one relationship (a pay group can only have one supervisor and an employee can only be the supervisor of one pay group). According to this post on GitHub, it is not possible to have a foreign key on a table with a different primary key. I've added the foreign

How to add an index on multiple columns with ASC/DESC sort using the Fluent API?

点点圈 提交于 2019-11-27 07:07:18
问题 I have a MVC ASP.NET application using Entity Framework 6 - Code First approach. Using the Fluent API, how can I add an index on multiple columns with ASC/DESC sort that is different for each column ? I've seen many examples using multiple columns but no way to set the sort order of the columns in the index. Table ----- Id Type DateFor DateCreated Value I want an index on the following columns: Type(ASC), DateFor(Desc), DateCreated(Desc). 回答1: Short answer: Entity Framework 6 does not allow

Fluent API, many-to-many in Entity Framework Core

感情迁移 提交于 2019-11-27 07:06:16
I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, Code first and Fluent API. A simple scenario would be: public class Person { public Person() { Clubs = new HashSet<Club>(); } public int PersonId { get; set; } public virtual ICollection<Club> Clubs { get; set; } } public class Club { public Club() { Persons = new HashSet<Person>(); } public int ClubId { get; set; } public virtual ICollection<Person> Persons { get; set; } } Please correct me if im wrong but I could honestly not find a question that contains an elaborate explanation on

How to create Lookup table and define relationships

荒凉一梦 提交于 2019-11-26 21:55:58
问题 As you can see at below, there are a Lookup table for the enum values and I want to create a relationship between a table's enum values and LookupKey column of the Lookup table (instead of ID column of the Lookup table). Lookup table: ID | LookupType | LookupKey | LookupValue | 101 | Status | 0 | Passive | 106 | Gender | 1 | Male | 113 | Status | 1 | Active | 114 | Gender | 2 | Female | 118 | Status | 2 | Cancelled | Main Table: ID | Status | Gender | Name | ... 1 | 0 | 1 | John Smith | ... 2

Cascade delete using Fluent API

好久不见. 提交于 2019-11-26 21:42:28
问题 I have two entities. Profile and ProfileImages . After fetching a Profile I want to delete ProfileImages through Profile without it just removing the reference to Profile (setting it to null ). How can this be done with fluent API and Cascading Delete? Do I set the HasRequired attribute or the CascadeDelete attribute? public class Profile { //other code here for entity public virtual ICollection<ProfileImage> ProfileImages { get; set; } } public class ProfileImage { // other code here left