Add List of EntityB as Property to EntityA that has no relation to EntityB - ef-code-first

I have a database first ef core project
There is an Entity 'Application' and an Entity 'Roles' without a relation
There is an Entity 'UserRoles' that has all the relations between 'Application' and 'Roles' and 'Users'
I can load all the referenced UserRoles in the Application Entity with
context.Applications.Include(inc => inc.UserRoles)
But can I load all existing Roles into the 'Application' Entity - and if, how is this called?
Or do I have to create an additional Entity? (It's needed for a Web-Navigation Control)

Nevermind, I changed the Database Model and added a Relation between Application and Role

Related

entity framework code first using existing database

I have started learning Entity framework and have a problem. I have created a code first mvc project and I don't want to drop the database when the model changes. What can I do other than DropCreateDatabaseIfModelChanges in my initializer class so that the database and the tables stays along with the data?
You don't need to use an initializer at all. This is valid:
Database.SetInitializer<YourContext>(null);
And in your configuration file set AutomaticMigrationsEnabled = false;
Then you can do the migrations yourself.

Creating entity instance in doctrine migrations

I'm having this problem:
In a symfony2 application using Doctrine, during the first dev milestone, I created a migration to update the database schema. The migration I created was container-aware in order to be able to create standard entries in the entity manager (a Config entity instance) in the postUp method. This worked very well until today, when I created a new migration that adds fields in the config entity.
When I run doctrine:migrations:migrate here is what happens:
The first migration runs it's up() method, tables are created according to milstone#1 mapping info
The first migration's postUp() method is called, and it uses the entity manager to try creating a new config instance. But since at this point the second migration was not run, persisting the instance fails because database schema is not in sync with entity mapping info.
Is there any way to work-around this problem ?

exception after upgrade ASP.NET Identity to 2.0

my project: VS2013, Entity Framework, Web forms, database first, Identity
I updated all NuGet packages of my project today (2014-4-15). Among them, Identity is upgraded to 2.0.0.0.
I thought things were going good, but unfortunately when I run the application, the following statement gives an exception.
namespace xxx.Models
{
// You can add User data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyConnection")
{
}
}
...
}
The exception information is as follows. It asks me to do Code First Migration. But my project is a Database First webforms project. How can I solve this problem? Thanks!
An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll but was not handled in user code
Additional information: The model backing the 'ApplicationDbContext' context has changed since the database was created.
This could have happened because the model used by ASP.NET Identity Framework has changed or the model being used in your application has changed.
To resolve this issue, you need to update your database. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=301867).
Before you update your database using Code First Migrations, please disable the schema consistency check for ASP.NET Identity by setting throwIfV1Schema = false in the constructor of your ApplicationDbContext in your application.
public ApplicationDbContext() : base("ApplicationServices", throwIfV1Schema:false)
You need to disable the schema consistency by doing what the error says. This is one time thing that happens when you upgrade from version 1.0 to 2.0.
public ApplicationDbContext() : base("MyConnection", throwIfV1Schema:false)
Next step - do the migrations.
Everything should work after that and you can remove this throwIfV1Schema:false
You can also take a look at this for more info
The problem is here :
public class ApplicationUser : IdentityUser
{
}
I think you should change to partial class to extend entity in Entity Framework. The reason is that EF will generate proxy class for each entity to connect to database.
The partial class should be write in the same namespace.

SPA Template VS2013 custom properties IdentityUser

I'm trying to add custom properties to the Identity User 2013 Spa Template. The Identity User model is not in the project when I F12 I get the model in the assembly which is not editable. I have read the tutorial to add a birth date to the Identity model however this example speaks about changing the Identity User model from a Web Forms prospective and it seems to have a UserIdentity model to edit. How do I add custom properties to the AspNetUser table?
I found the same. Most of the SPA template resources online seem to relate to the earlier (VS2012) version of the template which is quite confusing. However, by comparing the code generated by the MVC template I worked out what I needed to do which was.
In the Models folder create your own "ApplicationIdentity" and "ApplicationDbContext" classes. Include any additional properties that you want in your ApplicationIdentity class definition. In the example code below I have added a string for the email address.
using Microsoft.AspNet.Identity.EntityFramework;
namespace SimpleSPA.Models {
public class ApplicationUser: IdentityUser
{
public string Email { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext():base("DefaultConnection")
{
}
}
}
Use the solution Search and Replace to replace all references to "IdentityUser" with your new ApplicationUser.
Update the Startup static constructor in Startup.Auth.cs so that the UserManagerFactory initialisation includes your new ApplicationDbContext as below.
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
Rebuild the solution, check it builds OK.
Enable Entity Framework Code First migrations using the "Enable-Migrations" command in the Package Manager (PM) Console.
Add the initial migration using the PM command "Add-Migration Initial".
Initialise the database using the PM command "Update-Database".
Following this if you connect to the generated database you should see that the AspNetUsers table includes columns for your additional ApplicationUser properties. I haven't gone much further yet but I would expect that I will be able to access my new properties by using ApplicationUser instead the base IdentityUser.

Doctrine 2 with Symfony DI Container

I am working on a Zend Framework project that leverages Doctrine 2. I'm trying to get Symfony's DI container working with the project as well, but I'm having trouble. Suppose I have a class "Foo" that needs two instances injected. I can set up a Symfony DI container no problem to fetch me a new "Foo" with the dependencies injected. Great! But now what if I want to make "Foo" a Doctrine entity? All is well when I insert the entity to the DB because I can grab a new one from the DI container and simply persist the entity. However, when I query the entity from the DB Doctrine is instantiating my "Foo" instances and they will not have the proper dependencies injected. How do I get Doctrine to use my DI container so that the entity will have the appropriate dependencies? I know that Doctrine offers a "postLoad" hook in that I could use to inject dependencies on my entity, but that kind of defeats the purpose and benefit of the DI container.
A Doctrine Entity is a newable, not an injectable. Entities are not supposed to be created through a DIC. See this following blog post on the difference between newable and injectable:
http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/
Its a bit tricky, but it can be done. There exists a patch for symfony di container which allows you to pass a already existing object to the container builder instance and let the di container configure it for you (e. g. inject dependencies based on interfaces). The patch is implemented in this repository on github: https://github.com/lstrojny/symfony but didn´t make it upstream to symfony master repository.
You use it like this:
$user = new User();
$container->configure('someId', $user);
Then you could register a PostLoad event handler with Doctrine´s EventManager (see here for more details: http://www.doctrine-project.org/docs/orm/2.0/en/reference/events.html ). In this event handler you configure the loaded entity via the aforementioned method. It´s obvious but you cant use constructor injection in this case, only setter.
This is a bit tricky to set up, but can be very powerful especially in conjunction with the ability of symfony di container to inject dependencies based on interfaces.

Resources