ajr
ajr

Reputation: 974

Entity Framework 7 (core) Find entity based on composite key?

Is there a method or workaround to find entity based on composite key? When using Entity Framework 7 (core).

modelBuilder.Entity<Car>()
                .HasKey(c => new { c.State, c.LicensePlate });

Specially to avoid UNIQUE constrain exceptions with many-to-many intermediate table.

Upvotes: 1

Views: 429

Answers (2)

dzitkowskik
dzitkowskik

Reputation: 291

You can use also Find method after 1.1 release:

var entity = _context.Cars.Find(firstKey, secondKey);

Upvotes: 3

Fernando de Bem
Fernando de Bem

Reputation: 116

Usually I use a Context with DBSets of the entities. Then I can do something like:

 _context.Cars.Where(c => c.State != null && c.LicensePlate != null).ToList(); 

Or you check against whatever value would be "null". != 0.

Upvotes: 1

Related Questions