Reputation: 2061
Here is an interesting article on extending and customizing code first models in EF. As far as the extending the object for insert is concerned, Rowan has made it very clear.
ModelCustomizer.RegisterModelCustomization(
mb =>
{
mb.Entity<MyBizCustomer>();
});
ModelCustomizer.RegisterTypeSubstitution<Customer, MyBizCustomer>();
var service = new CustomerService();
var customer = new MyBizCustomer { FirstName = "Jane", LastName = "Doe", IsVIP = true };
service.AddCustomer(customer);
Now, my problem is, if I want to use MyBizCustomer to query something with the extended model, how can I do that. How can I query the model if I want to get all customers where IsVIP = true ?
Is there any more elegant way to allow the developers extend the existing model instead of using some key-value pair concept to implement dynamic fields?
Upvotes: 1
Views: 466
Reputation: 2090
You can use the OfTYpe LINQ method to filter to entities that are MyBizCustomers and then apply your additional filter:
query.OfType<MyBizCustomer>().Where(c => c.IsVip)
Upvotes: 1