我正在尝试根据另一个彼此之间没有关系的实体在我的实体中设置一个属性。该实体从数据库设置属性,我希望从数据库中的另一个表设置属性之一,但是我无法深入了解它。
例如,
public class First {
[Key]
public int ProdId { get; set; }
public string Supplier { get; set; }
public virtual ICollection<Log> Logs { get; set; }
[NotMapped]
public bool IsSavedForLater
{
get
{
return Logs.Where(l =>
{
var content = l.LogContent.JsonStringToObject<History>();
return (content.ProdId == ProdId && l.TableName == "Condition");
}).Any();
}
}
}
如您所见,属性IsSavedForLater是[NotMapped]
,我希望从日志中设置此属性,
这是日志实体,
public class Log
{
[Key]
public int LogId { get; set; }
public string LogContent { get; set; }
public string TableName { get; set; }
public DateTime LogDate { get; set; }
public string BlameName { get; set; }
public bool? Deleted { get; set; }
}
是否可以像这样在没有任何数据库关系的情况下进行导航?
伙计们,我花了很多时间才找到解决方案。
我所做的是,我首先在First Class中添加了另一个属性,如下所示
public class First {
[Key]
public int ProdId { get; set; }
public string Supplier { get; set; }
public string TableName { get; set; }
public virtual ICollection<Log> Logs { get; set; }
}
如您所见,First类具有一个新属性TableName
,该属性也在我的Logs类中,
public class Log
{
[Key]
public int LogId { get; set; }
public string LogContent { get; set; }
public string TableName { get; set; }
public DateTime LogDate { get; set; }
public string BlameName { get; set; }
public bool? Deleted { get; set; }
}
然后,我在db上下文类中添加了一个像这样的模型构建器,
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SmuForecasting>().HasKey(x => new { x.TableName }).HasMany(x => x.PsLogs).WithOptional().HasForeignKey(x => new { x.TableName });
}
这对我来说非常好,但有副作用,它取代了开始时设置的原始密钥,因此我最终采用了完全不同的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句