简体   繁体   English

ASP.NET Boilerplate上的组合主键

[英]Composive Primary Key on ASP.NET Boilerplate

I'd like to know if using ASP.NET Boilerplate with EF, is it possible: 我想知道是否可以将ASP.NET Boilerplate与EF结合使用,是否可能:

  1. Use the composive Primary Key in EF. 使用EF中的组合主键。 I have seen many samples with only one column in TPrimaryKey in Repository. 我在存储库中的TPrimaryKey中看到了很多只有一列的示例。
  2. The column's name of Primary Key is required call "Id". 主键的列名称是必需的,称为“ Id”。

I searched online, but I can't find the answer. 我在网上搜索,但找不到答案。

An example about your question 有关您的问题的示例

public class MyEntity : Entity<string>
{
    [NotMapped]
    public override string Id
    {
        get { return MyIntField + "-" + MyStringField ; }
        set { /* no need set */ }
    }

    [Key]
    [Column(Order = 1)]
    public virtual int MyIntField { get; set; }

    [Key]
    [Column(Order = 2)]
    public virtual string MyStringField { get; set; }

    public virtual string OtherField { get; set; }
}

And then use your repository like this 然后像这样使用您的存储库

   public class MyEntityAppService: IMyEntityAppService {
    private readonly IRepository < MyEntity, string > _myEntityRepository;

    public AccountAppService(IRepository < MyEntity, string > myEntityRepository) {
     _myEntityRepository = myEntityRepository;
    }

    public void Test() {
     _myEntityRepository.Insert(new MyEntity {
                                   MyIntField = 1, 
                                   MyStringField = "test", 
                                   OtherField = "Alper Ebicoglu"
                                });

     var myAllEntities = _myEntityRepository.GetAllList();

     var myFilteredEntity = _myEntityRepository.Single(s => s.MyIntField == 1 &&
                                                    s.MyStringField == "test");

    }

    // ...
   }

PS: some repository methods may not work as expected. PS:某些存储库方法可能无法按预期工作。 so before implementing the solution test all repository methods 因此,在实施解决方案之前,请测试所有存储库方法

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

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