1

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?

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I created the main query in the CustomerService.cs: public IQueryable<Customer> GetData() { var db = new CustomerStalkerContext(); var customer = ModelCustomizer.Create<Customer>(); var data = db.Customers; return data; } Not getting any output data. Am I doing something wrong here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.