具有前缀实体框架的表的习惯约定 [英] Custom Convention for table with Prefix Entity Framework

查看:415
本文介绍了具有前缀实体框架的表的习惯约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的所有POCO类都有两个字母前缀LK_。主要和实体框架公约外键不行。考虑到我有200个课程用Key或ForeignKey属性进行装饰,这是一个繁琐的过程&听起来不像一个聪明的方式。



请问您可以建议定制会议吗?

  public class LK_Employee 
{
public Guid EmployeeID {get; set;}
public string Name {get; set;}
}

public class LK_Company
{
public Guid CompanyID {get; set;}
public string Name {get; set;}
}

public class LK_Employee_LK_Company
{
public Guid EmployeeID {get; set;}
public Guid CompanyID {get; set;}
}


解决方案

当有一个简单的列键时,任何像 LK_TableName 这样的文件作为表的主键:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties&Guid;()
.Where(p =>LK_+ p.Name == p.DeclaringType.Name +Id)
.Configure(p => p.IsKey());
}

为了支持复合键以及简单的kesy,您需要这样做:

  //计数器:跟踪复合键内列的顺序
var tableKeys = new Dictionary< ;类型,INT>();

modelBuilder.Properties&Guid;>()
.Where(p =>
{
//打破段中的entiy名称
var段= p.DeclaringType.Name.Split(new [] {LK _,_ LK_},
StringSplitOptions.RemoveEmptyEntries);
//如果属性具有像其中一个段的名称,那么键的一部分
if(segments.Any(s => s +ID== p.Name))
{
//如果它还没有在列计数器中,添加
if(!tableKeys.ContainsKey(p.DeclaringType))
{
tableKeys [p.DeclaringType] = 0;
}
//增加计数器
tableKeys [p.DeclaringType] = tableKeys [p.DeclaringType] + 1;
return true;
}
return false;
})
。配置(a =>
{
a.IsKey();
//使用计数器设置复合键中的列的顺序
a.HasColumnOrder(tableKeys [a.ClrPropertyInfo.DeclaringType ]);
});

创建前提键的约定要复杂得多。您可以在此路由中查看EF6约定: / src / EntityFramework.Core / Metadata / Conventions / Internal / ForeignKeyPropertyDiscoveryConvention.cs //github.com/aspnet/EntityFramework/rel =nofollow> EF6 github 。并且看到使用说明的测试: / test / EntityFramework.Core.Tests / Metadata / ModelConventions / ForeignKeyPropertyDiscoveryConventionTest.cs


All my POCO classes have two letter prefix LK_. Entity framework conventions for primary & foreign keys would not work. Considering I have ~200 classes to decorate with Key or ForeignKey attribute its a cumbersome process & does not sound like a smart way of doing it.

Could you please suggest custom convention?

public class LK_Employee
{
    public Guid EmployeeID {get; set;}
    public string Name {get; set;}      
}

public class LK_Company
{
    public Guid CompanyID {get; set;}
    public string Name {get; set;}      
}

public class LK_Employee_LK_Company
{
    public Guid EmployeeID {get; set;}      
    public Guid CompanyID{get; set;}        
}

解决方案

This will set any filed like LK_TableName as the table's primary key, when there is a simple column key:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Properties<Guid>()
        .Where(p => "LK_" + p.Name == p.DeclaringType.Name + "Id")
        .Configure(p => p.IsKey());
}

To support composite keys, as well as simple kesy, you need to do this:

// Counter: keeps track of the order of the column inside the composite key
var tableKeys = new Dictionary<Type,int>();

modelBuilder.Properties<Guid>()
.Where(p =>
{
    // Break the entiy name in segments
    var segments = p.DeclaringType.Name.Split(new[] {"LK_","_LK_"},
                      StringSplitOptions.RemoveEmptyEntries);
    // if the property has a name like one of the segments, it's part of the key
    if (segments.Any(s => s + "ID" == p.Name))
    {
        //  If it's not already in the column counter, adds it
        if (!tableKeys.ContainsKey(p.DeclaringType))
        {
            tableKeys[p.DeclaringType] = 0;
        }
        // increases the counter
        tableKeys[p.DeclaringType] = tableKeys[p.DeclaringType] + 1;
        return true;
    }
    return false;
})
.Configure(a =>
{
    a.IsKey();
    // use the counter to set the order of the column in the composite key
    a.HasColumnOrder(tableKeys[a.ClrPropertyInfo.DeclaringType]);
});

Creating the convention for foreing keys is much more complex. You can have a look at EF6 convention in this route: / src/ EntityFramework.Core/ Metadata/ Conventions/ Internal/ ForeignKeyPropertyDiscoveryConvention.cs, on EF6 github. And see the tests for illustration of use: / test/ EntityFramework.Core.Tests/ Metadata/ ModelConventions/ ForeignKeyPropertyDiscoveryConventionTest.cs

这篇关于具有前缀实体框架的表的习惯约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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