【发布时间】:2015-08-06 16:04:41
【问题描述】:
这是我的模型:
public class RepoDocument
{
public DocumentRecord Document { get; set; }
public string Title { get; set; }
public string Path { get; set; }
}
这是子类定义:
public class DocumentRecord
{
public Guid Id { get; set; }
public string Name { get; set; }
// ... Other fields here...
}
我需要什么
我想让Id 作为RepoDocument 模型的主键。
我在我的数据库上下文中使用此代码:
public class DocumentsContext : DbContext
{
public DocumentsContext()
: base("name=DbConnectionString")
{
}
public DbSet<RepoDocument> DocumentsTable { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<RepoDocument>().HasKey<Guid>(c => c.Document.Id);
}
}
问题
但是,当我在包管理器控制台中运行 add-migration 时,我收到以下错误:
属性表达式“c => c.Document.Id”无效。该表达式应表示一个属性:C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'。指定多个属性时使用匿名类型:C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'。
问题
如何使用fluent API将我的模型子类的属性设置为数据库的主键?
【问题讨论】:
-
这不是你定义子类的方式。正如您所定义的,RepoDocument 具有 DocumentRecord。如果您的意思是 RepoDocument 是 DocumentRecord,则不应将 DocumentRecord 声明为成员,而应将 DocumentRecord 作为超类包含在您的 RepoDocument 声明中:
public class RepoDocument : DocumentRecord -
@JerryFederspiel 我认为这只是一个无效的命名。子类作为在另一个内部使用的类:)
-
是的,子类是指一个类作为另一个类的属性。
-
@AliSharabiani,不过,您不应该以不正确的方式使用术语。
Subclass是继承关系的稳定命名,不是聚合。
标签: c# entity-framework ef-fluent-api