0

I'd like to know if using ASP.NET Boilerplate with EF, is it possible:

  1. Use the composive Primary Key in EF. I have seen many samples with only one column in TPrimaryKey in Repository.
  2. The column's name of Primary Key is required call "Id".

I searched online, but I can't find the answer.

Julio Spader
  • 349
  • 4
  • 7

1 Answers1

2

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. so before implementing the solution test all repository methods

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55