如何将复杂类型包含在实体索引中? [英] How do I include a complex type to an Entity Index?

查看:136
本文介绍了如何将复杂类型包含在实体索引中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这2个POCO ...

I have these 2 POCOs...

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; }
}

我创建了跟踪文件与其文件之间的1到多个关系。我想添加一个索引,使它可以使SqlTraceFiles对其SqlTrace是唯一的;允许多个SqlTraceFiles被命名为相同的,只要它们属于不同的SqlTrace。

I created a 1 to many relationship between the trace and its files. I want to add an index that would make it so that SqlTraceFiles are unique to its SqlTrace; Allow multiple SqlTraceFiles to be named the same as long as they belong to a different SqlTrace.

下面是我在SqlTraceFileConfiguration中的内容:EntityTypeConfiguration< SqlTraceFile>

Below is what I have within the SqlTraceFileConfiguration : EntityTypeConfiguration< SqlTraceFile >

Property(TraceFile => TraceFile.Name)
            .IsRequired()
            .HasColumnAnnotation("Index", new IndexAnnotation(
                new IndexAttribute("IX_SQLTracefile_FileTrace", 1) { IsUnique = true }
                ));
Property(TraceFile => TraceFile.Trace)
            .HasColumnAnnotation("Index", new IndexAnnotation(
                new IndexAttribute("IX_SQLTracefile_FileTrace", 1) { IsUnique = true }
                ));

我的问题是它不采取'tracefile => tracefile.trace'我猜该实体想要外键代替'tracefile.trace'。有没有一个模式我必须遵循完成这个?或者是我的职位的解决方法。

My problem is that it doesn't take the 'tracefile => tracefile.trace' I am guessing that entity want the foreign key in place of 'tracefile.trace'. Is there a pattern I must follow to accomplish this? Or a workaround to my position.

推荐答案

尝试这样做你的poco:

Try this as your poco's:

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; }
}

这样不需要流畅的代码。

You won't need the fluent code this way.

这篇关于如何将复杂类型包含在实体索引中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆