My understanding is that the convention is to use the Entity Framework Fluent API in side of the overridden OnModelCreating method in your DBContext method; and that this is only run during db creation.
This would leave me to believe that the Fluent API can only be leveraged more than once(for instance while you're developing your db) if you are dropping and creating your database everytime there are changes.
My question is: Is there a way to leverage the Fluent API for db configuration, for database updates only, for example while running the database migration command "update-database"?
From the DbContext.OnModelCreating documentation:
Typically, this method is called only once when the first instance of a derived context is created. The model for that context is then cached and is for all further instances of the context in the app domain.
So it is called when the model is created, not just when the database is created. That will happen when you call Update-Database after adding a migration, or when you start up your website after publishing a new version.
References:
https://stackoverflow.com/a/6181867/150342
http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/
Related
At my job we're gradually replacing a monolithic legacy system with a microservices architecture. I've been tasked with writing an auth server using Asp.Net Core, Identity Server 4 and Entity Framework*. The legacy system already has auth and our deadline is approaching, so we're going to use the legacy system as a backend for the time being.
How can I set up Identity Server/Entity Framework to pull login info through the legacy system? So far, everything I've found involves adding a database like SQL server. Assume for the sake of argument I'm not able to pull data directly from the MySQL database that the legacy system uses, but it is easy to get the user data via a JSON API.
I have written a DbContext and an implementation of IProfileService which uses it, but I'm not sure how to actually pull the users in the DbContext, and when I try to sign in from a client I get this error:
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.
However I haven't been able to find/figure out what to put in DbContext.OnConfiguring to set this up. I suppose I need to implement IServiceProvider somewhere, but can't find any details of how to do so.
*We're not married to these so suggestions for something more appropriate are welcome. We are using .Net Core.
The EF bit seems like a red herring here. If you're talking to an API in a legacy system then you won't use EF for that at all.
If using IdentityServer4 then it makes sense to use their EF implementations for the configuration and operational stores and then implement your sign in UI, IClaimsService etc using the API exposed by your existing system. To do that just create a simple client implementation that calls said API and accepts and returns whatever models you require.
I have a service that use EF 6.1.3 to access to the database. I have POCO entities to store the results of EF, but the resutls are dynamicProxies instead of the POCO entity.
The problem is that when the service try to send to the client this dynamic proxy, I get an error receiving the http response.
I have tried to disabled the creation of the proxies entities in my dbContext, and then I receive my real POCO entity so I have no problems.
But really I don't know what is dynamic proxies and when is usuful to use them and when I can disabled them.
EDIT: I have disabled lazy loading.
Thanks.
When creating instances of POCO entity types, the Entity Framework
often creates instances of a dynamically generated derived type that
acts as a proxy for the entity. This proxy overrides some virtual
properties of the entity to insert hooks for performing actions
automatically when the property is accessed. For example, this
mechanism is used to support lazy loading of relationships.
Source: https://msdn.microsoft.com/en-us/data/jj592886.aspx
You will find everything you need in the above article!
I've started an ASP.NET Web App project using the template for an Azure Mobile Service and tried to create my model the Model-First approach.
After generating my database from my finished model I proceeded to add a TableController class for one of my entities to test my project.
But when I tried to make a POST request I got this message:
Model compatibility cannot be checked because the DbContext instance
was not created using Code First patterns. DbContext instances created
from an ObjectContext or using an EDMX file cannot be checked for
compatibility.","exceptionType":"System.NotSupportedException
Is it not possible to create an Azure Mobile Service with Model-First at all? What are my options if I want to use the Model-First approach?
If it's not an existing model, I would switch to Code First. It is possible to use Model First, but it requires more manual configuration.
If you have an existing model, see this tutorial on how to add the right system properties and map to data transfer objects: http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-use-existing-sql-database/
How can I use MVC4 Migrations without using Entity Frameworks? I would really like to use data migrations but I am not using Entity Frameworks. I am using dapper-dot-net.
Yes, you can use Migrations without using Entity Framework. All Migrations cares about is the metadata it uses to manage the database and you need to use some EF stuff to handle that, but you then don't ever need to use EF to actually access the database. This blog post describes the process in detail: http://weblogs.asp.net/fredriknormen/archive/2012/02/15/using-entity-framework-4-3-database-migration-for-any-project.aspx
Check out Insight.Database.Schema on NuGet. It gives you a lot of the magic of migrations without the hassle of EF. I'll be updating the docs on github over the next few days.
I need to use the users table from a parent site, is it possible to map the user table using the fluent API and have it be read only?
In case of EF code first you can't directly make it "read-only" but you can design your code to not expose DbContext and related DbSet outside of your DAL logic so rest of the application cannot add a new user.
In case of EDMX based mapping you can make it read only by mapping it as a custom SQL query (or database view) where insert, update and delete operations are not supported until you map them custom SQL commands or stored procedures.
According to a comment on ScottGu's blog, no.
Right now we don’t support read-only (or semi-read-only) properties in
Code First but this is an interesting suggestion that I will talk with
our design team about. In general the Entity Framework doesn’t have
support for marking something as “read-only” so we may have to wait
for another major .NET framework release to get this working. For now,
you will have to add business logic in your entity to allow the
property to be set only once.
Jeff Derstadt
Entity Framework Code First Team