C# 如何将复杂类型包含到实体索引中?

C# 如何将复杂类型包含到实体索引中?,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我有这两个POCO public class SqlTrace { public int id { get; set; } public string Name { get; set; } public virtual List<SqlTraceFile> TraceFiles { get; set; } } public class SqlTraceFile { public int id { get; set; } public stri

我有这两个POCO

public class SqlTrace
{
    public int id { get; set; }
    public string Name { get; set; }
    public virtual List<SqlTraceFile> TraceFiles { get; set; }
}

public class SqlTraceFile
{
    public int id { get; set; }
    public string Name { get; set; }
    public virtual SqlTrace Trace { get; set; }
}

我的问题是它没有使用'tracefile=>tracefile.trace'我猜那个实体想要外键代替'tracefile.trace'。有没有一个模式,我必须遵循,以完成这一点?或者我的职位的变通办法。

请尝试以下作为您的poco:

public class SqlTrace
{
    public int Id { get; set; }

    //added indexes with annotations
    [Index("otherNameIndex", IsUnique = true)]
    public string Name { get; set; }

    //changed to ICollection
    public virtual ICollection<SqlTraceFile> TraceFiles { get; set; }
}

public class SqlTraceFile
{
    public int Id { get; set; }
    [Index("nameIndex", IsUnique = true)]
    public string Name { get; set; }

    //added the FK id property explicit and put an index on to it.
    [Index("indexname", IsUnique = true)]
    public int SqlTraceId {get;set;}
    public virtual SqlTrace Trace { get; set; }
}
公共类SqlTrace
{
公共int Id{get;set;}
//添加了带注释的索引
[索引(“otherNameIndex”,IsUnique=true)]
公共字符串名称{get;set;}
//更改为ICollection
公共虚拟ICollection跟踪文件{get;set;}
}
公共类SqlTraceFile
{
公共int Id{get;set;}
[索引(“nameIndex”,IsUnique=true)]
公共字符串名称{get;set;}
//添加了FK id属性explicit并在其上放置索引。
[索引(“indexname”,IsUnique=true)]
public int SqlTraceId{get;set;}
公共虚拟SqlTrace跟踪{get;set;}
}

您不需要这样的fluent代码。

您可以使用此处提到的属性创建索引:您可以将索引放在显式创建的FK属性上。使用fluent API和DataNotation无法做到这一点?我认为fluent也可以,但我更喜欢注释,所以我不太清楚。创建FK id显式是一个好主意,这样您就可以在流畅的代码中访问它。我接受了答案。。。我真的很希望用FluentAPI完成它。你提到我可以显式地添加外键,我不知道你可以这么做。我认为这将是我的方法。非常感谢您的快速回复和回答。没问题,如果您找到了流畅的方式;你可以随时把它贴出来作为你自己的答案。我相信您会得到一些支持:-)@Hakubex:最后一句话:在这个场景中,
SqlTraceId
通过命名约定(
SqlTrace
+
Id
)被设置为
SqlTrace
的键。您也可以选择任意名称,但随后需要
[ForeignKey]
属性。
public class SqlTrace
{
    public int Id { get; set; }

    //added indexes with annotations
    [Index("otherNameIndex", IsUnique = true)]
    public string Name { get; set; }

    //changed to ICollection
    public virtual ICollection<SqlTraceFile> TraceFiles { get; set; }
}

public class SqlTraceFile
{
    public int Id { get; set; }
    [Index("nameIndex", IsUnique = true)]
    public string Name { get; set; }

    //added the FK id property explicit and put an index on to it.
    [Index("indexname", IsUnique = true)]
    public int SqlTraceId {get;set;}
    public virtual SqlTrace Trace { get; set; }
}