IharYakimush / comminity-data-odata-linq

Use OData filter text query in linq expresson for any IQuerable without ASP.NET dependency. Support netstandard2.0
Other
42 stars 16 forks source link

InvalidOperationException: The entity does not have a key defined #5

Closed wadebee closed 6 years ago

wadebee commented 6 years ago

By convention the entity key property is Id. How do I modify my EntityType EDM registration to reflect my domain key (eg: builder.EntityType.HasKey(t => t.MyDomainKey)?

wadebee commented 6 years ago

Ok, I figured this out looking at the source code. Looks like the IQueryable.OData extension method allows you to pass in an IEdmModel. I declared my types (with custom keys) and passed it in. Query filtered as expected.

Thanks, Wade

IharYakimush commented 6 years ago

Hi Wade,

Also you can use System.ComponentModel.DataAnnotations.KeyAttribute to define an entity key.

    using System.ComponentModel.DataAnnotations;

    public class SampleWithCustomKey
    {       
        [Key]
        public string MyDomainKey { get; set; }

        public DateTime DateTime { get; set; }
    }

Thanks, Ihar

mehdipanjwani-ains commented 1 year ago

[Key] attribute works. But when scaffolding (db first) the entity model is recreated automatically which reverts the manual changes.

Is it possible to specify the key during OnModelCreatingPartial?

partial void OnModelCreatingPartial(ModelBuilder modelBuilder) {
    modelBuilder.Entity<SampleWithCustomKey>().HasKey(x => x.MyDomainKey);
}