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

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

问题描述

我有这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.

推荐答案

在您的TraceFile poco中添加一个属性 SqlTraceId 它将表示外键的id,并使用该属性创建索引。所以,在你的导航属性上添加数据注释 [ForeignKey =SqlTraceId] 和你的唯一索引注释位置在名称 SqlTraceId 属性。使用数据注释可以使用这个例子来实现:

In your TraceFile poco add one more property SqlTraceId which will represent id for the foreign key and use that property to create index. So, on your navigation property add data annotation [ForeignKey="SqlTraceId "] and your unique index annotation place on the Name and SqlTraceId properties. Using data annotations you can achieve that using this example:

    public class SqlTraceFile
    {
      public int id { get; set; }
      [Index("IX_SQLTracefile_FileTrace", 1, IsUnique = true)]
      public string Name { get; set; }
      [Index("IX_SQLTracefile_FileTrace", 2, IsUnique = true)]
      public int SqlTraceId { get; set; }
      [ForeignKey("SqlTraceId ")]
      public virtual SqlTrace Trace { get; set; }
   }

我认为您可以使用FluentAPI而不是数据注释,但是主要的是你得到想法如何解决这个问题。

I think you can do the same using FluentAPI instead of data annotations, but the main thing is that you get idea how to solve this problem

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

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