ef-code-first

EntityFramework CodeFirst: CASCADE DELETE for same table many-to-many relationship

旧街凉风 提交于 2019-12-30 04:28:07
问题 I have an entry removal problem with the EntityFramework and a many-to-many relationship for the same entity. Consider this simple example: Entity: public class UserEntity { // ... public virtual Collection<UserEntity> Friends { get; set; } } Fluent API Configuration: modelBuilder.Entity<UserEntity>() .HasMany(u => u.Friends) .WithMany() .Map(m => { m.MapLeftKey("UserId"); m.MapRightKey("FriendId"); m.ToTable("FriendshipRelation"); }); Am I correct, that it is not possible to define the

What is the best way to manually generate Primary Keys in Entity Framework 4.1 Code First

浪子不回头ぞ 提交于 2019-12-30 03:18:11
问题 What is the best way to manually generate Primary Keys in Entity Framework 4.1 Code First? I am programming ASP.NET MVC 3 and I use a repository pattern. I currently generate keys in a sequential order by using the code below: 'Code First Class Public Class Foo <Key()> <DatabaseGenerated(DatabaseGeneratedOption.None)> Public Property iId As Integer Public Property sBar As String End Class 'Context Class Public Class FooBarContext : Inherits DbContext Public Property Foos As DbSet(Of Foo) End

How do I define Foreign Key Optional Relationships in FluentAPI/Data Annotations with the Entity Framework?

不问归期 提交于 2019-12-30 02:38:27
问题 I have a (sample) application with the following code: public class Posts { [Key] [Required] public int ID { get; set; } [Required] public string TypeOfPost { get; set; } public int PollID { get; set; } public virtual Poll Poll { get; set; } public int PostID { get; set; } public virtual Post Post { get; set; } } Basically, I don't know if there is a better way of doing this, but, I have a list of Posts, and, people can choose if it is a Poll or a Post , As Entity Framework doesn't work with

How should I seed data to many to many relation in Entity Framework Code first 5.0

送分小仙女□ 提交于 2019-12-30 01:12:30
问题 I am having my first steps in EF 5.0 I have a many to many relationship. Movie can Have multiple Types and Type can have multiple Movies public class Movie { public int MovieId { get; set; } [Required] public string Name { get; set; } public virtual ICollection<Type> Types { get; set; } } public class Type { public int TypeId { get; set; } public string MovieType { get; set; } public virtual ICollection<Movie> Movies { get; set; } } When I generated the Database it creates many-to-many

EF 1 to 1..* relationship

廉价感情. 提交于 2019-12-29 09:59:47
问题 Is there a way to create a one to one-or-more relationship in Entity Framework? I've seen plenty examples showing a 1 to 0..* relationship, but I want to be sure that in the given example Foo can only exist if it has at least one Bar. class Foo { List<Bar> Bars { get; set; } } class Bar { public Foo Foo { get; set; } } I see that this is not easily achieved by SQL since I want a kind of NOT NULL at the Foo table instead of at the Bar table, but can Entity Framework handle this? I realized I

Entity Framework Code First truncating my decimals

不想你离开。 提交于 2019-12-29 07:28:27
问题 I am using Entity Framework 6.x using the Code First approach on an MVC 5 application. In this particular situation my model (among other things) contains two properties named Latitude and Longitude: [Required, Range(-90, +90)] public decimal Latitude { get; set; } [Required, Range(-180, +180)] public decimal Longitude { get; set; } And when I performed the migration I got something like this CreateTable("ResProperty"), c => new { : Latitude = c.Decimal(nullable: false, precision: 10, scale:

Entity Framework | Code First | Mapping of sub property from CultureInfo.Name

ε祈祈猫儿з 提交于 2019-12-29 06:35:10
问题 I've got an entity like this : public class Course { public CultureInfo Culture {get; set;} : } And I want to map just the Name property of CultureInfo to a single column in the table that Entity Framework generates for me. How would I go about this? Currently I've tried looking at the OnModelCreating() method of the DbContext, but I'm not finding anything that is standing out. I'm using Entity Framework 4.1 in and MVC 3 application with a Code First approach. Many thanks. 回答1: Unfrotunatelly

EF Code First - WithMany()

自闭症网瘾萝莉.ら 提交于 2019-12-29 05:18:07
问题 I recently came by the class ManyNavigationPropertyConfiguration<TEntity, TTarget> , and within that class there I found a method named WithMany() with 2 overloads. The first overload: WithMany() Configures the relationship to be many:many without a navigation property on the other side of the relationship. The second overload: WithMany(Expression<Func<TTarget, ICollection<TEntity>>>) Configures the relationship to be many:many with a navigation property on the other side of the relationship.

Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser?

好久不见. 提交于 2019-12-28 16:05:43
问题 I'm in VS 2013 and have just created an MVC application. I'm creating an object I intend to have a foreign key to the AspNetUsers table in the resulting database. The project does have an ApplicationUser (deriving from IdentityUser) that looks like a property-column match with the AspNetUsers table. How do we properly declare a foreign key to this? public MyObject { public string UserId { get; set; } [ForeignKey("UserId")] public ApplicationUser User { get; set;} // other properties } Now, I

Cascade Delete Rule in EF 4.1 Code First when using Shared Primary Key Association

旧街凉风 提交于 2019-12-28 16:02:48
问题 I implemented a bidirectional 1:1 relationship based on this answer: Primary /Foreign Key in Entity Framework I define the bidirectional relation this way: public class Student { public virtual int StudentId { get; set; } public virtual Anamnesis Anamnesis { get; set; } . . . } public class Anamnesis { [Key, ForeignKey("Student")] public int AnamnesisId { get; set; } public virtual Student Student { get; set; } . . . } where, Student is the principal entity and Anamnesis it the entity that