简体   繁体   English

具有前缀实体框架的表的自定义约定

[英]Custom Convention for table with Prefix Entity Framework

All my POCO classes have two letter prefix LK_. 我所有的POCO类都有两个字母前缀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. 考虑到我有200个要用Key或ForeignKey属性进行装饰的类,这是一个麻烦的过程,而且听起来并不明智。

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: 当有一个简单的列键时,这会将诸如LK_TableName之类的任何文件设置为表的主键:

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: 要支持复合键以及简单的kesy,您需要执行以下操作:

// 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 . 您可以在以下路径中查看EF6约定: / src/ EntityFramework.Core/ Metadata/ Conventions/ Internal/ ForeignKeyPropertyDiscoveryConvention.cs ,在EF6 github上 And see the tests for illustration of use: / test/ EntityFramework.Core.Tests/ Metadata/ ModelConventions/ ForeignKeyPropertyDiscoveryConventionTest.cs 并查看测试以获取使用说明: / test/ EntityFramework.Core.Tests/ Metadata/ ModelConventions/ ForeignKeyPropertyDiscoveryConventionTest.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM