Entity Framework CodeFirst Multiple type of users - entity-framework

I have task to build application for user registration. I have 3 types of user (profiles)
1. "Normal" user
2. "Company" user
3. "Company2" user - similar like 2. but with few additional fields..
All users share some specific info, like e-mail and password (Login data), role, registration date etc.... So, I'm trying to "design" classes for this type of app using only EF Code First approach, but with no luck..
Do I need table (class) for :
USER - all kind of users with all their Login data (email and password) and UserType
USERTYPE - list of all user types (1,2,3)
USER_DETAILS - details of normal user
COMPANY_DETAILS - details of company
COMPANY2_DETAILS -details of company2
My problem is how to reference USER table with USER_DETAILS, COMPANY_DETAILS, COMPANY2_DETAILS. I hope so that you understand my problem :)
My classes (user management and profile) is implemented like http://codefirstmembership.codeplex.com/ example.
Thank you in advance!

You can use a normal inheritance model with Entity Framework.
I would just use a base class User that contains the login data (please don't store the password in the DB though, use a hash - Membership should do this for you already) and other user info, then you can add another class CompanyUser that inherits from User and contains the additional properties. Finally CompanyUser2 (needs a better name) can inherit from CompanyUser.
Having this in place there are different models you can use on how EF maps your classes to tables:
1.) Table-per-Hierarchy
2.) Table-per-Type
3.) Table per Concrete Type

Related

Microsoft Master Data Services 2016 Additonal Domain Atrribute Referencing

Is it possible to reference additional columns apart from the 'Code' and 'Name' columns when using a domain attribute in an entity?
E.g. A person entity has a code of '1' and a name of 'Smith' and a Gender of 'Male'
In a customer entity there is a domain value referencing the person entity which displays the following 1 {Smith}. The users would like an additional read only attribute which would copy the Gender value of 'Male' into the customer entity based on the domain value. Can this be done using out of the box MDS UI?
I know this is duplicate data and breaks normal form but for usability this would be useful. It would be the equivalent of referencing additional columns in an MS Access drop down list.
Many thanks in advance for any help
This is not possible with the standard UI. One option would be to develop a custom UI where you can handle these kind of requests.
If you want to stick with the standard product I can see a workaround but this is a bit of a "dirty" one.
You can misuse (abuse) the Name attribute of the Person entity by adding a business rule to the Person entity that generates the content of the Name attribute as a concatenation of multiple attributes. You of course need an additional attribute that serves as a place holder for the original Name. The concatenated field will then show in your customer entity.
One question that does come to mind is why a user would like/need to see the gender of a person in a customer list? As you have a separate Person entity I expect you to have multiple persons per customers. What would the gender of one person - even if it is the main contact - matter?

Yii2 adding parameters to the model with a function

I have two types of data. Articles and article types. They are stored a postgres DB. Every article instance should have particular type. All articles have common properties like "name", "create_date" and so on that are in every article and fields that are specific for the specific type like "some_image", "ingredients", and so on for the type1 and "images" and "points" for type2.
The additional properties for the specific type are stored in a single json column in the db table.
When the url "some_path/articles/create/2" is opened a form for creating an article of type 2 is displayed
I have an Articles ActiveRecord model and ArticlesController.
My problem is that in the model I have only have the common properties. And the additional properties are always different.
I need a method in the model or in the controller with which I can add the additional properties with the proper rules and all (like the common properties in the model) and then in the view I can render all fields without difference.
Is there a way this to be done and how?
I read how this can be done with Javascript, but I want it to be dobe before page rendering, not after that
May be this can help.
Use Scenario in your model
Model class has scenario function which you can inherit.
User Register Scenario - Required - username, password, email
User Login Scenario - Required - username, email
public function scenarios() {
$scenarios = parent::scenarios();
$scenarios['login'] = ['name','password'];//Scenario Values Only Accepted
return $scenarios;
}
and you can use it as

Entity Framework code first aspnet_Users mapping / joins

I was wondering with Entity Framework 4.1 code first how do you guys handle queries that involve an existing aspnet_Users table?
Basically I have a requirement for a query that involves the aspnet_Users so that I can return the username:
SELECT t.Prop1, u.Username
FROM Table1 t
INNER JOIN aspnet_User u ON t.UserId = u.UserId
Where t.Prop2 = true
Ideally in linq I would like:
from t in context.Table1
join u in context.aspnet_Users on t.UserId equals u.UserId
where t.Prop2 = true
But I'm not sure how to get aspnet_Users mapping to a class User? how do I make aspnet_Users part of my dbset ?
Any help would be appreciated, thanks in advance
Don't map aspnet_Users table or any other table related to aspnet. These tables have their own data access and their own logic for accessing. Mapping these tables will introduce code duplication, possible problems and breaks separation of concerns. If you need users for queries, create view with only needed information like id, user name, email and map the view. The point is that view will be read only, it will contain only allowed data and your application will not accidentally modify these data without using ASP.NET API.
First read Ladislav's answer. If you still want to go ahead : to do what you want would involve mapping the users and roles and members tables into the codefirst domain - which means writing a membership provider in code-first.
Luckily there is a project for that http://codefirstmembership.codeplex.com/ although its not a perfect implementation. The original is VB, look in the Discussion tab for my work on getting it running in c# MVC.
I'm working with the author on a better implementation that protects the membership data (password, last logged on date, all of the non-allowed data) but allow you to map and extend the user table. But its not ready yet!
You don't really need to use Entity Framework to access aspnet_membership provider accounts. You really just need to create an instance of the membership object, pass in a unique user identifier and a Boolean value indicating whether to update the LastActivityDate value for the user and the method returns a MembershipUser object populated with current values from the data source for the specified user.
You can then access the username by using the property of "Username".
Example:
private MembershipUser user =
Membership.GetUser(7578ec40-9e91-4458-b3d6-0a69dee82c6e, True);
Response.Write(user.UserName);
In case you have additional questions about MembershipProvider, you can read up on it on the MSDN website under the title of "Managing Users by Using Membership".
Hope this helps you some with your requirement.

Entity Framework inheritance from SQL Membership

I am using Entity Framework 3.5. I have created an object that calls "Persons" that is inherited from SQL Membership table (aspnet_Users), they are linked by UserId (Guid) with 1-to-(0..1) relationship and they both belong to "UserSet".
Say, I have an existing "aspnet_User" called "jsmith" in the membership database already. How can I create just the "Person" (child) that links it to "jsmith" in entity framework?
Guid guid = new Guid("xxxx-xxxx-xxx..") // jsmith Guid
User user = GetUserByGuid(guid); // Assuming a function gets "jsmith" as "User"
// Try 1:
Person person = new Person();
person.UserId = guid;
context.AddToUserSet(person);
context.SaveChanges(); // This doesn't work and throws an error
// Try 2:
Person.CreatePerson(person); // Doesn't work either, because it creates a whole new user
context.SaveChanges(); // Throws an error
I even tried to create an EntityKey and use detach/attach, didn't work either. Am I doing anything wrong? Any help is appreciated.
R.B.
You should not put SQL membership into your entity model. ASP.NET membership is intended to be pluggable; you can swap out the SQL membership provider for, say, an OpenID or domain authentication provider. Mapping the SQL membership tables makes you totally dependent on the specific implementation of one version of that provider, which is bad coupling.
Also, even if you did do this, inheritance is the wrong relationship. A user has an account. A user is not an account. So the correct relationship is composition, not inheritance.
Perhaps "Person" is the wrong relationship in the example. However, if you are talking about extending ASP.NET memembership by adding more properties like "FirstName", "LastName", etc.. this is not totally wrong. There are some articles talk about extending SQL membership and/or creating "Custom Membership" by adding your own db table and then inherited from base MembershipUser class. In that case, it is inheritance.
There are some articles that talks about extending membership, but not on Entity though:
http://www.code-magazine.com/Article.aspx?quickid=0703071
More articles
- codesmart.wordpress.com/2009/03/27/extending-the-microsoft-aspnet-membership-provider/
Also, you may want to consider using Profiles as alternative.

Adding a property to an Entity Framework Entity from another table

I'm just starting out with the Entity Framework and ADO.NET Data Services and I've run into an issue that I can't seem to figure out. I have two tables, one that has user information and the other that has a created by field. Within the database, there isn't a foreign key between these tables. The user table contains an arbitrary Id, a username, and a display name. The created by field contains the user's username. In my entity I would like to have the user's display name since this is what I need to display and expose over the ADO.NET Data Service? I'm aware that I could restructure the database, but I was hoping that I could do the join using the username as I would in a SQL statement.
Thanks in advance,
-Damien
You can make a view using a join of both tables, and then use this object to display the user's name.
There's some info on mapping custom queries here.