How to create my own table using EF in asp net core identity - entity-framework-core

Currently, I have EF working perfectly fine with asp net core identity. My web app allows users to login in 2 ways: using their local account and using their azure active directory account. However, only the allowed azure directory accounts are allowed to log in. Therefore, I need to create another table to hold those accounts to cross check when they log in.

If you want to create a new table for the Identity project, I suggest you could try to modify the identity EF's dbcontext with the new create model class.
More details, you could refer to below codes:
public class UserRelationship
{
public int UserId { get; set; }
}
Modify the ApplicationDbContext :
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<UserRelationship> UserRelationships { get; set; }
}
Then you could enable the migration and update the database like this article shows.

Related

Entity Framework Core migrations with identity db context

I've setup a .net core api app to host JWT token authentication using an asp identity database. That part works great. My problem is when I go to add custom classes, every migration I perform is trying to create the AspNet* tables again even though they exist. The migration is aware of my custom class (Test) and wants to create it, but the script it's trying to run dies on the AspNetRoles table (already exists error).
I create a new migration:
Add-Migration NewMigration
Then update:
Update-Database
Here is my ApplicationDBContext.cs:
public class ApplicationDBContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
{
}
// This is my custom class
public DbSet<Test> Test { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
I'm at a loss. I just need to do migrations and have EF Core ignore the AspNet identity tables.
Edit: Here is ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
}
Migrations folder:

Entity Framework combine custom tables and identity tables

I have created a business layer and data layer for a custom task management system. It just has 2 related tables Tasks and TaskNotes. Now, I would like to build an MVC application on top of it using the Individual User Accounts that you can specify for authentication (ASP.NET Identity).
What is the easiest way to get my tables and the identity tables into one database?
The way to do this is to have one DbContext for all your entities.
You should add Task and TaskNote to your ApplicationDbContext which inherited from IdentityDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Tasks> Tasks { set; get; }
public DbSet<TaskNotes> TaskNotes { set; get; }
}

Filter Context data by user input

I am using MVC with Entity framework.
I have a Client requirement that client want to set default period for all the context table. When he set some date range into the user setting page it saves the detail to the sql table. When the app runs application all the data from table will come for that particular period.
I want to apply this filter in my context file.
Here is my context file and suppose i got the date here.
public class MyContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
public DbSet<Contact_phone> Contact_phones{ get; set; }
}
How i achieve this.
Thanks

Azure Mobile Apps - POST to table with foreign key "Bad Request" 400 error

I'm hoping this is a simple question. I've created an Azure Mobile Apps project based upon the sample ToDo project, adding my own tables/data objects. The problem I'm having is adding/POSTing records to a table that has a foreign key relationship to another. The following is my Employee table data object:
public class Employee : EntityData
{
public string Name { get; set; }
public string EmailAddress { get; set; }
public bool IsActive { get; set; }
public string EmployeeTypeId { get; set; }
public virtual EmployeeType EmployeeType { get; set; }
}
...and this is my EmployeeType data object:
public class EmployeeType : EntityData
{
public string EmpType { get; set; }
public bool IsActive { get; set; }
}
The virtual EmployeeType property in the Employee class was necessary, I believe, to create the relationship with the EmployeeType table when using EF Code First to create the tables in the database. (At least, that's what I understand, and it worked) I am able to insert records from my Xamarin client app into the EmployeeType table using the InsertAsync method, but I receive a "Bad Request" 400 error when trying to insert into the Employee table.
I've looked around quite a bit for solutions, but everything refers to Azure Mobile Services and not Apps. If need be, I can update this question with my client side model classes (I'm on my PC now and don't have access to the Xamarin Studio project on my Mac). For reference, these classes are pretty much the same as the data objects - just each property is decorated with the JsonProperty attribute, except the virtual property outlined in the service. And for completeness, I did try adding that property to the client object and it still threw the "Bad Request" 400 error.
Thanks for any direction you can offer me.
Most likely, the problem is happening when trying to map the foreign key. Are you specifying all of the fields for employee type? I recommend that you do the following:
Use Fiddler or attach a delegating handler to your client to see what the outgoing request looks like. Update your comment with the JSON body. See https://github.com/Azure/azure-mobile-apps/wiki/Help,-my-app-isn't-working!#log-outgoing-requests-in-managed-client-xamarin-windows.
Attach a debugger to your server project. You can do this while running locally or after your solution is deployed to Azure, but you'll have better performance if you run locally. See https://github.com/Azure/azure-mobile-apps/wiki/Help,-my-app-isn't-working!#remote-debugging-net-server-sdk.
I suspect that the problem is that EmployeeType ends up being null in your deserialized object, and then Entity Framework rejects the DB insert.
Could you get more information from the bad request? Try adding this to the table controller
protected override void Initialize(HttpControllerContext controllerContext)
{
controllerContext.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
}

Entity Framework 4 with Existing Domain Model

Im currently looking at migrating from fluent nHibernate to ADO.Net Entity Framework 4.
I have a project containing the domain model (pocos) which I was using for nHibernate mappings. Ive read in blogs that it is possible to use my existing domain model with EF4 but ive seen no examples of it. Ive seen examples of T4 code generation with EF4 but havent come accross an example which shows how to use existing domain model objects with EF4. Im a newby with EF4 and would like to see some samples on how to get this done.
Thanks
Aiyaz
Quick walkthrough :
Create an entity data model (.edmx) in Visual Studio, and clear the "custom tool" property of the edmx file to prevent code generation
Create the entities in your entity data model with the same names as your domain classes. The entity properties should also have the same names and types as in the domain classes
Create a class inherited from ObjectContext to expose the entities (typically in the same project as the .edmx file)
In that class, create a property of type ObjectSet<TEntity> for each of you entities
Sample code :
public class SalesContext : ObjectContext
{
public SalesContext(string connectionString, string defaultContainerName)
: base(connectionString, defaultContainerName)
{
this.Customers = CreateObjectSet<Customer>();
this.Products = CreateObjectSet<Product>();
this.Orders = CreateObjectSet<Order>();
this.OrderDetails = CreateObjectSet<OrderDetail>();
}
public ObjectSet<Customer> Customers { get; private set; }
public ObjectSet<Product> Products { get; private set; }
public ObjectSet<Order> Orders { get; private set; }
public ObjectSet<OrderDetail> OrderDetails { get; private set; }
}
That's about it...
Important notice : if you use the automatic proxy creation for change tracking (ContextOptions.ProxyCreationEnabled, which is true by default), the properties of your domain classes must be virtual. This is necessary because the proxies generated by EF 4.0 will override them to implement change tracking.
If you don't want to use automatic proxy creation, you will need to handle change tracking yourself. See this MSDN page for details