Entity Framework 1 Flashcards

1
Q

What is the purpose of IModelCustomizer? Give the declaration of the interface.

A

Performs additional configuration of the model in addition to what is discovered by convention.

public interface IModelCustomizer
{
public void Customize(ModelBuilder modelBuilder, DbContext context);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the use case of IModelCustomizer?

A

It’s useful when you want to change the model, but have no access to the DbContext implementation code.

This is why this interface is used very often by database providers and different extensions, but almost never used in application code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to replace the default IModelCustomizer implementation with your own?

A

You can call ReplaceService method of DbContextOptionsBuilder.

The object is accessible as a parameter when you call AddDbContext on IServiceCollection.

Example:

builder.ReplaceService<IModelCustomizer, MyModeCustomizer>();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the lifetime of IModelCustomizer? What are the implications of that?

A

The service lifetime is Singleton.

Implications:
- This means a single instance is used by many DbContext instances.
- The implementation must be thread-safe.
- This service cannot depend on services registered as Scoped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the recommendation when you replace the IModelCustomizer implementation?

A

When replacing this service consider deriving the implementation from ModelCustomizer or RelationalModelCustomizer to preserve the default behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What ModelCustomizer, one of the default implementations of IModelCustomizer, does?

A

Builds the model for a given context. This implementation builds the model by calling OnModelCreating(ModelBuilder) on the context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What RelationalModelCustomizer, one of the default implementations of IModelCustomizer, does?

A

It’s kind of strange, but it just inherits ModelCustomizer and has no additional logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly