Entity Formwork中这种数据关系,怎么创建MAP映射

[复制链接]
查看11 | 回复2 | 2021-1-27 07:19:33 | 显示全部楼层 |阅读模式
最近做一项目,想变革一下原来的思路,在统一Create,UPdate,Delete日志记录时,我的设计是这样的
tb_OpLog(Id,parentId,datetime,userId,type)其中type是个tyint,在CodeModel中是个enum
publicenumOpType
{
Create=0,
Update=1,
Delete=2
}
抽象根
publicabstractclassEntity
{
publicEntity()
{
Id=Infrastructure.GuidHelper.NewGuid();
}
publicSystem.GuidId{get;set;}
}
操作日志Model
publicclassOperationLog:Entity
{
privateOPType_Type=OPType.Create;
publicOperationLog(OPTypetype)
{
this.ParentId=Infrastructure.GuidHelper.NewGuid();
this.UserId=Infrastructure.GuidHelper.NewGuid();
this._Type=type;
}
///
///操作时间
///
publicDateTimeDateTime
{
get;set;
}
///
///外键所有关联表的ID
///
publicGuidParentId
{
get;set;
}
///
///类型0Create,1UPdate,2Delete
///
publicvirtualOPTypeType
{
get;set;
}
///
///操作用户编号
///
publicGuidUserId
{
get;set;
}
}

假如我现在有一个文章表tb_Article(Id,title,Content)对应的Model
publicclassOperationLog:Entity
{
publicOperationLog(OPTypetype)
{
this.Title=String.Empty;
this.Content=String.Empty;
this.Create=newOperationLog(OpType.Create);
}
///
///文章标题
///
publicDateTimeTitle
{
get;set;
}
///
///文章内容
///
publicStringContent
{
get;set;
}
///
///创建操作人,不为空
///
publicOperationLogCreate
{
get;set;
}
///
///最后更新操作人,可以为空
///
publicOperationLogUpdate
{
get;set;
}
///
///删除操作人,可以为空
///
publicOperationLogDelete
{
get;set;
}
}

现在我用EF做ORM,在Article的Maping中,不知道该如何设置这三个OpLog的关系,求助懂的朋友们
publicpartialclassArticleMap
:System.Data.Entity.ModelConfiguration.EntityTypeConfiguration
{
publicArticleMap()
{
//table
ToTable("Frm_Article","dbo");
//keys
HasKey(t=>t.Id);
//Properties
Property(t=>t.Id)
.HasColumnName("Id")
.IsRequired();
Property(t=>t.ParentId)
.HasColumnName("F_ParentId")
.IsRequired();
Property(t=>t.Title)
.HasColumnName("F_Title")
.HasMaxLength(255)
.IsRequired();
  //Create不能为空
?????
//Update可以为空
??????????
//Delete可以为空
??????????
}

分 -->
回复

使用道具 举报

千问 | 2021-1-27 07:19:33 | 显示全部楼层
那3个属于导航属性你在百度里搜:配置EF导航属性相关的文章有很多
回复

使用道具 举报

千问 | 2021-1-27 07:19:33 | 显示全部楼层
使用DataAnnotations:
//第一联系人
[InverseProperty("PrimaryContactFor")]
publicPersonPrimaryContact{get;set;}
//第二联系人
[InverseProperty("SecondaryContactFor")]
publicPersonSecondaryContact{get;set;}
或使用FluentAPI:
modelBuilder.Entity().HasOptional(l=>l.PrimaryContact).WithMany(p=>p.PrimaryContactFor);
modelBuilder.Entity().HasOptional(l=>l.SecondaryContact).WithMany(p=>p.SecondaryContactFor);
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行