csharp EF6 - 多对多第二种方式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp EF6 - 多对多第二种方式相关的知识,希望对你有一定的参考价值。
namespace EntityFramework.Entity
{
public class BaseEntity
{
public int Id { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime ModifiedTime { get; set; }
}
public class Student : BaseEntity
{
public string Name { get; set; }
public byte Age { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}
public class Course : BaseEntity
{
public string Name { get; set; }
public int MaximumStrength { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}
public class StudentCourse : BaseEntity
{
public int StudentId { get; set; }
public virtual Student Student { get; set; }
public int CourseId { get; set; }
public virtual Course Course { get; set; }
}
}
namespace EntityFramework.Map
{
public class StudentMap : EntityTypeConfiguration<Student>
{
public StudentMap()
{
// table
ToTable("Students");
// key
HasKey(t => t.Id);
// fields
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// relationship
HasMany(m => m.StudentCourses)
.WithRequired(o => o.Student)
.HasForeignKey(k => k.StudentId);
}
}
public class CourseMap : EntityTypeConfiguration<Course>
{
public CourseMap()
{
// table
ToTable("Courses");
// key
HasKey(k => k.Id);
// relationship
HasMany(m => m.StudentCourses)
.WithRequired(o => o.Course)
.HasForeignKey(k => k.CourseId);
}
}
}
namespace EntityFramework
{
public class EfDbContext : DbContext
{
public EfDbContext() : base("name=ConnectionString")
{
Database.SetInitializer(new CreateDatabaseIfNotExists<EfDbContext>());
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<StudentCourse> StudentCourses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !string.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType
&& type.BaseType.GetGenericTypeDefinition() ==
typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
base.OnModelCreating(modelBuilder);
}
}
class Program
{
static void Main(string[] args)
{
using (var ctx = new EfDbContext())
{
ctx.Database.Log = Console.WriteLine;
var student = new Student
{
Name = "Jeffcky",
Age = 26,
CreatedTime = DateTime.Now,
ModifiedTime = DateTime.Now
};
var course1 = new Course
{
Name = "C#",
MaximumStrength = 12,
CreatedTime = DateTime.Now,
ModifiedTime = DateTime.Now
};
var course2 = new Course
{
Name = "EntityFramework 6.x",
MaximumStrength = 12,
CreatedTime = DateTime.Now,
ModifiedTime = DateTime.Now
};
var studentCourse1 = new StudentCourse
{
Student = student,
Course = course1,
CreatedTime = DateTime.Now,
ModifiedTime = DateTime.Now
};
var studentCourse2 = new StudentCourse
{
Student = student,
Course = course2,
CreatedTime = DateTime.Now,
ModifiedTime = DateTime.Now
};
ctx.StudentCourses.Add(studentCourse1);
ctx.StudentCourses.Add(studentCourse2);
ctx.SaveChanges();
}
Console.ReadKey();
}
}
}
以上是关于csharp EF6 - 多对多第二种方式的主要内容,如果未能解决你的问题,请参考以下文章