Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
907 views
in Technique[技术] by (71.8m points)

c# - Entity Framework 6. Disable ModelCaching

OK, Google, I am not able to googling it. Documentation says

The model for that context is then cached and is for all further instances of the context in the app domain. This caching can be disabled by setting the ModelCaching property on the given ModelBuidler

and SO confirms it. But i can't find way to do it. I have to disable caching because i want to get data from several log table with same structure via just one model, so my code looks like

logTableNames.ForEach(n =>
{
    using (var context = new LogContext(n))
    {
        Console.WriteLine($"Project: {n} -- {context.Logs.Count()} rows.
");
    }
});

and configure with

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Log>().ToTable(_tableName);
}

but 'OnModelCreating' method called just once and i can not reconfigure table mapping. So maybe there is another true-way to do my task?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the answer from the Program Manager of the Entity Framework team.

Rowan Miller (MSFT)

We removed CacheForContextType in CTP5, we originally intended it to be used when folks wanted to use the same context in the same AppDomain with different models. The issue is that it would create the model on every initialization and didn't allow any way to cache a series of models and choose which one to use during each initialization. Model creation is expensive so we wanted to promote a better pattern.

The pattern we recommend is to externally create a ModelBuilder -> DbDatabaseMapping -> DbModel for each model you want to use. The DbModel should be cached and used to create context instances. The ModelBuilder -> DbModel workflow is a little messy and the class names aren't great, they will be tidied up for RTM.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...