I am coding an API that uses the Identity feature on asp.net and right now I am trying to make a method that returns all the couples ( user.id, role.id ) which are stored on the table AspNetUserRoles when assigning a role to a user.
One solution would be to get the full list of users and find each role of that user in a double loop but having too much data, it wouldn't be optimal for my case. The perfect solution for me would be to access that table directly which contains exactly what I am looking for
Thanks for the help!
You will have to create class models and relationships explicitly as described in the documentation below:
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-3.1#add-user-and-role-navigation-properties
This documentation expects you to at least create the ApplicationUser, ApplicationRole and ApplicationUserRole class models. Also, you will have to update your DBContext accordingly to correctly use the new models that you create. The names for the models can be changed. However, in any case, this should not change any DB schema and will not require any EF migrations.
Lastly,
Related
I want to restrict creating or modifying the data model schema for backendless. Specifically, only want to create or modify the data model schema using the console. The data model schema should not be modified using the API.
How can this be achieved?
It this point it can be achieved by you making sure that your code stays consistent and does not introduce new fields/properties. Adding a new field to a class and then saving an instance of the class with the API will result in a new column being created. So to avoid that make sure that your classes at any point of time represent the schema of the backend.
a customer has an existing database. The schema is often changed within the database itself (e.g. he adds a new column).
My task is to develop an admin area with symfony that automatically reacts on table schema changes without modifying the application code. E.g. the customer adds a new column to table "MyEntity", and the application automatically generates a new column in the accordingly list view.
My approach is to dynamically map the table columns to the Entity class so that ALL Attributes and ALL Getters/Setters are generated dynamically from the table schema.
So is it possible to map the table columns in a Doctrine Entity without the use of Annotations or XML Files.
Something like:
class MyEntity{
public function generateMappingFromSchema($sTableName){...}
}
Please don't do that. Doctrine was not designed for such use case.
There is a library though you should check https://github.com/laravel-doctrine/fluent which basically is a mapping driver that allows you to manage your mappings in an Object Oriented approach. And there are other tools:
http://crud-admin-generator.com/
http://crudkit.com/
http://www.grocerycrud.com/
which are maybe better for that, I don't know.
But again, please don't do that. Do not allow the customer to modify the database schema or give them e.g. a phpMyAdmin which was designed for that.
I am working on asp.net MVC 5 application. I am using Identity for creating users. I am using it first time and creating first mvc 5 application. I have two types of users. I want two tables for both types of users. So first record should be inserted in aspnetuser table and then in related tables and aspnetuser table should be linked with both tables so that i can access them. I have created models for both types of users. How can i link them with identity in model ?
Please suggest
Well, first, you can only truly have one user, at least in terms of the stuff Identity does. You can subclass that single user to create other types, though. So, essentially, you'd have a setup like:
public class ApplicationUser : IdentityUser
{
...
}
public class FooUser : ApplicationUser
{
...
}
public class BarUser : ApplicationUser
{
...
}
public class BazUser : ApplicationUser
{
...
}
In other words, just one class inherits directly from IdentityUser, while all your various types will inherit from that one class (in this case, ApplicationUser).
By default, Entity Framework handles inheritance via STI or Single Table Inheritance. How this works is that you'll have just one table AspNetUsers (the default table name for the Identity user), and any properties your subclasses add will be added as columns on that table. This means that all your subclass properties have to be nullable, since if a different subclass or just the base ApplicationUser class are persisted that property wouldn't be set.
If that's not acceptable, there's other inheritance strategies you can implement, namely TPT (Table-Per-Type) and TPC (Table-Per-Concrete Type). In TPT, the base, shared properties go all in one table and then each subtype has its own table with just columns for the properties it adds. In TPC, every class gets it's own table for everything, so each of your individual subtypes would have all of the Identity columns added. I don't believe this particular option would be compatible with Identity, though, or at the very least it would be highly burdensome as all the foreign key relationships between things like roles and claims would have to be duplicated for each table. Even if you could hack Identity to make that work, you'd lose any cohesive way of doing anything with all users, regardless of type, at once.
Basically, your options are the default, also known as TPH (Table-Per-Hierarchy), or TPT. However, TPT is less efficient because it essentially requires a join for every query just to get the full instance. If you absolutely need to have non-nullable properties on your subclass, it might be an appropriate option. However, remember that the property only needs to be nullable at the database-level. If you go with TPH, you can still require that those properties have values via validation in the application.
To create my model i followed the below steps
I have already have the database so i added an ADO model to my Project under Model folder.
ADO model created the classes for me and i can use them.
I have created a view(not View of MVC) in my DB. But when i am trying to add my ADO model it is failed since it has no Primary key.
Now i am trying to access my DbView. What is the best solution for this ? I am not able to use LinqtoSql since my DB is Oracle.
The other option that i can write a model class to access my DbView ? But how can i write ?
Regards
I have solved this problem. If one of my column has "not null" property, EF define this coloum n as a PK. So i define a new column in my view and generate GUID for this column.
I think it will help someone .
Regards.
I want to create a relationship between a custom table (Websites) and the default aspnet tables related to Users.
I'm using code-first so for most FK relationships I would just do
public ModelName ModelName { get; set; }
With this, EF will automatically create the FK relationships. Very easy.
What's confusing is the most effective way to hook into the aspnet users/membership table. Do I create a new model Users that acts as an interface so that I can implement custom user code?
Is there a best way to do this that fits well into EF best practices? I basically just want to relate a user to the Websites table/model so that EF can do its thing.
"Do I create a new model Users that acts as an interface so that I can implement custom user code?"
If you want flexibility, I would say this is the way to go. This way it would be easier if you wanted to change to some sort of different Authentication DB structure in the future.
For example, have an "AppUser" Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way you can simply add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.
https://github.com/TroyGoode/MembershipStarterKit
Hope this helps!
This has few preconditions:
ASP.NET tables must be in the same database as your own tables
Previous precondition means that you must either create your database and tables manually (without automatic code-first generation) or you must use some custom initializer which will add non mapped ASP.NET tables as part of database recreation
If you want your model class to have relation with ASP.NET table you must model ASP.NET table as another entity. I'm not sure if you can use ASP.NET classes for that because for example MembershipUser doesn't have parameterless public constructor which is required for EF. So you will most probably need to create duplicate classes and their mappings and use these classes when referencing ASP.NET entities.