1 Star 0 Fork 0

debuglife/CodeFirstApp

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Maps.cs 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
debuglife 提交于 2016-07-10 12:59 +08:00 . first codefirstapp
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace firstCodeFirstApp
{
public class DonatorMap : EntityTypeConfiguration<Donator>
{
public DonatorMap()
{
ToTable("Donators");
Property(m => m.Name)
.IsRequired();
HasMany(d => d.PayWays)
.WithRequired()
.HasForeignKey(p => p.DonatorId);
}
}
public class DonatorTypeMap : EntityTypeConfiguration<DonatorType>
{
public DonatorTypeMap()
{
HasMany(d => d.Donators)
.WithOptional(d => d.DonatorType) //外键约束可以为空
.HasForeignKey(d => d.DonatorTypeId)
.WillCascadeOnDelete(false);
}
}
public class StudentMap : EntityTypeConfiguration<Student>
{
public StudentMap()
{
HasRequired(s => s.Person)
.WithOptional(p => p.Student); // Optional 可选的
HasKey(s => s.PersonId); //使用 HasKey 方法 指定一个表的主键
Property(s => s.CollegeName)
.HasMaxLength(50)
.IsRequired();
}
}
public class PersonMap : EntityTypeConfiguration<Person>
{
public PersonMap()
{
/*
* * MapLeftKey 配置左外键的列名。左外键指向在 HasMany 调用中指定的导航属性的父实体。
* MapRightKey 配置右外键的列名。右外键指向在 WithMany 调用中指定的导航属性的父实体。
*/
HasMany(p => p.Companies)
.WithMany(c => c.Persons)
.Map(m => {
m.MapLeftKey("PersonId");
m.MapRightKey("CompanyId");
});
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zanpen2000/CodeFirstApp.git
git@gitee.com:zanpen2000/CodeFirstApp.git
zanpen2000
CodeFirstApp
CodeFirstApp
master

搜索帮助