DbSet doesn't have a Find method in EF7

后端 未结 11 1766
醉话见心
醉话见心 2020-12-03 04:28

I am trying to create a generic repository to access my database. In EF6 I was able to do that in order to get a specific entity:

protected IDbSet d         


        
11条回答
  •  旧巷少年郎
    2020-12-03 05:09

    Not enough reputation to comment, but there is a bug in @Roger-Santana answer when using it in a console app/seperate assembly:

    var i = 0;
    foreach (var property in key.Properties)
    {
        entries = entries.Where(e => e.Property(property.Name).CurrentValue == keyValues[i]);
        i++;
    }
    var entry = entries.FirstOrDefault();
    

    The value of 'i' is captured in the foreach so that when entries.FirstOrDefault() is called, keyValues[i] has the value of (at least) keyValues[i++], which in my case crashed with an out of index error. A fix would be to copy the value of 'i' through the loop:

    var i = 0;
    foreach (var property in key.Properties)
    {
       var idx =i;
        entries = entries.Where(e =>  e.Property(property.Name).CurrentValue == keyValues[idx]);
        i++;
    }
    var entry = entries.FirstOrDefault();
    

提交回复
热议问题