MVC Model Confusion - sql-server

I have confusion regarding models when we have relations between multiple tables. Formerly, I was using views or stored procedures of SQL to manipulate data but now I am using Entity Framework so confused how my model should look like ?
I have a table for Users, UserImages, UserRoles.
UserImages and UserRoles is referring to UserID from Users table.I am not sure how my model should be. While displaying data I generally use a view created in SQL server by joining these 2 tables. And when an user is edited or created I update these 3 tables.
Not sure what should my model look like in this case ? Do I create a SQL server view and add it to edmx ? Or add 3 tables separately in EDMX and create custom properties in User model for those other 2 tables?
[HttpPost]
public ActionResult Create(UserFormViewModel UserView)
{
User user = UserView.User;
try {
if (ModelState.IsValid) {
repository.AddUser(user);
repository.Save();
return View(new UserFormViewModel(user));
} else {
return View(new UserFormViewModel(user));
}
} catch (Exception ex) {
ModelState.AddModelError("Error", ex.Message);
return View(new UserFormViewModel(user));
}
}
public class UserFormViewModel {
UucsrRepository repository = new UucsrRepository();
public User User { get; private set; }
public SelectList States { get; private set; }
public SelectList Genders { get; private set; }
public SelectList RolesLists { get; private set; }
public SelectList SelectedRolesLists{ get; private set; }
public UserFormViewModel(User contact) {
User = contact;
States = new SelectList(repository.GetAllStates() ,"ShortName","Name");
Genders = new SelectList(repository.GetAllGenders(), "Gender", "Description");
RolesLists = new SelectList(repository.GetAllRolesLists(), "Id", "Name");
}
}
I am not sure how should I exactly handle the Adding role list and images here .

The User class should have a list of Roles and Images. The tables should have foreign keys to each other by UserId. When generating the models from your tables select all the tables you want Entity Framework to use. The User model should in that case automatically have a List of UserRoles and UserImages. Change the names appropriately.
When adding or changing roles or images you should fetch the user and update or add them to the correct list.
That's how I would do it. In this case your User is an aggregate root. Check out DDD you're interested.

Related

Are there git hub projects available for reference to bring existing user Db to identity server 4? If there is one, point me to that

I have users Db built in Sql server and Have lot of columns based on application requirements. How do i use this users table in Identity server 4? Are there any git hub reference project to do this?
Create your own user class by inheriting from IdentityUser. There you can add your own properties and override the existing ones (to map them to your column names).
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
IsActive = true;
}
// these two properties are your custom ones
public int? StaffId { get; set; }
public bool IsActive { get; set; }
// here you map an Identity property to a column you already have
[Column("LockoutEndDateUtc")]
public override DateTimeOffset? LockoutEnd { get; set; }
}

Map List object using SQL Server and Dapper

I'm using SQL Server and Dapper and I want to properly store my models object into database and retrieve them.
That's my model, the guid list is list of other model 'Generator' IDs.
public class GeneratorSet
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Guid> GeneratorsList { get; set; }
}
My goal is to correctly map this object to a SQL Server table and then using Dapper correctly retrieve my objects from database. The relationship is many to many (set can 'posses' many generators, and generator may be possesed by many sets).
You can do this using the SplitOn parameter... Here is a guide;
https://taylorhutchison.github.io/2016/03/23/dapper-orm-complex-queries.html
Or just by having unique names and mapping using a lambda - using the example from the documentation (https://github.com/StackExchange/Dapper);
var sql =
#"select * from #Posts p
left join #Users u on u.Id = p.OwnerId
Order by p.Id";
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
var post = data.First();
Assert.Equal("Sams Post1", post.Content);
Assert.Equal(1, post.Id);
Assert.Equal("Sam", post.Owner.Name);
Assert.Equal(99, post.Owner.Id);
I can't write the actual code/query as I don't know your database schema... but hopefully you get the idea?

dreaded "Ids can not be autogenerated for entities with multipart keys"

Ok I’m at a loss, being new to breeze I’m still learning the ropes. My project uses the hot towel template for AngularJs and breeze from John Papa.
He's what I’m trying to achieve: I have a master\slave tables in my database. An "Agency" has many people it can "Notify". Here are the EF classes for the server side:
public class Agency {
public Agency() {
this.Notifies = new HashSet<Notify>();
}
public long Id { get; set; }
[Required, MaxLength(50)]
public string Name { get; set; }
<<removed unneeded details>>
public bool Active { get; set; }
public virtual ICollection<Notify> Notifies { get; set; }
}
public class Notify
{
public long Id { get; set; }
public long? AgencyId { get; set; }
public string Name { get; set; }
<<removed unneeded details>>
public virtual Agency Agency { get; set; }
}
Now the Maps:
public class AgencyMaps : EntityTypeConfiguration<Agency>
{
internal AgencyMaps()
{
HasKey(x => x.Id);
}
}
public class NotifyMap : EntityTypeConfiguration<Notify>
{
internal NotifyMap()
{
HasKey(x => x.Id);
HasOptional(x => x.Agency)
.WithMany(p => p.Notifies)
.HasForeignKey(i => i.AgencyId);
}
}
Now on the client side I use breeze to create new entities like this:
// create a new entity
function create() {
return manager.createEntity(entityName);
}
// create a new notify entity
function createNotify(){
return manager.createEntity(entityNameNotify);
}
Then there are two scenarios I need to achieve:
- First is where I retrieve an existing agency and add additional
people to notify
- Second is where I create a new agency and add people
to notify
Both fail in the same place.
Note: I’m using SQL server and my Id fields are bigint (long) at this point in time.
I’m retrieving the “Agency” entity and placing it in a variable called “vm.agency”. “vm.agency” has a navigation called “notifies” with an entity type of “Notify”. So when I want to create and add a new person I’m calling this function:
function addNotifyRec(){
if (vm.agency !== undefined){
var notifyRec = datacontext.agency.createNotify(); //<<< fails here
notifyRec.agencyId = vm.agency.id;
notifyRec.name = vm.notify.name;
<<removed unneeded details>>
vm.agency.notifies.push(notifyRec);
logSuccess(“New person to notify added”);
}
else
{ logError(“Agency is undefined”); }
}
As soon as the createNotify() is called I get the “Ids can not be autogenerated for entities with multipart keys” error.
So I’m stuck. It seems to me this is a pretty common scenario. I am obviously not understanding the breeze framework well enough to implement this. If you can point me in the right directions I’d appreciate your help.
UPDATE 4/9/2014
I'm thinking i could eliminate this issue altogether if i switch over to guid id and generate them client side. Is this correct thinking?
What's interesting here is that Breeze thinks that Notify.Id and Notify.AgencyId are multi part primary keys but they are actually not. Id is a PK and AgencyId is an FK. The only thing that I can think of is try removing the EntityTypeConfiguration for both Agency and Notify, specifically the part where it specifies HasKey and HasForeignKey. This Fluent API configuration shouldn't be required as EF will match your configuration by convention instead.
I took a different approach on working around my issue. Since i have the luxury to change out the id types, i swapped out the bigint ids to Uuid types and removed the auto generation of the ids in sql. Now i'm just creating my own ids using breeze.core.getUuid() when a new record is created. Not sure this is the most efficient way to work around the issue, but it seems to be working fine.

How do I map to a lookup table using Entity Framework Code First with Fluent API

I'm new to asp.net mvc and entity framework code first apporach, I'm not that keen on databases either. I apologise in advance for wrong terminology or the way I understand things.
Now to the question. I have the following models:
User Model
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public int RoleId { get; set; }
[ForeignKey("RoleId")]
public virtual IEnumerable<Role> Roles { get; set; }
}
Role Model
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
}
What i eventually want is a way to use Ef codefirst approach with the fluent API to map the UserId and RoleId to a User_Role table with a one-to-many relation, a user can have multiple roles:
I assume that what s done in this question is the right approach, except that there the author used a many-to-many connection. I tried it that way but the part with u => u.users gives me an error(I assume thats because there is no users property in the model, so he answered his question but didn't update his question?)
My question: What is the exact fluent api code to let Ef generate this table for me?
Things I'm unsure about: (Feel free to ignore)
Is this the right approach of my problem?
Once i have the lookup table, is this still the right way to declare my navigational property so i can later use it like user.Roles and retrieve their roles?
where will be the RoleId in the User model be populated from, Roles table or User_Role?
Has having an ID in the lookup table any use?
Thanks in advance! I really appreciate your expertise.
First you should get rid of the RoleId property in the User model. Having that as a foreign key tells which single role a user has. Since a user can have many roles, the foreign key should not be in the user table but in the mapping table.
So what you have is a many-to-many relationship between users and roles, and Entity Framework can create the needed mapping table automatically without you having to configure anything.
If you simply have a Roles property in the User entity and a Users property in the Role entity, EF will figure out that you want a many-to-many between those two, and create a table with the primary keys of both entities as a combined primary key, which will be used to map users to roles.
When loading a User from the database you can then use the Roles navigation property to figure out which roles the user has, and you can load a Role to figure out which users are in that role.
The simplest way of making it work would be something like this:
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
static Context()
{
Database.SetInitializer(new DropCreateDatabaseAlways<Context>());
}
public Context()
: base("Server=localhost;Initial Catalog=Test;Integrated Security=True;")
{
}
}
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public List<Role> Roles { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public List<User> Users { get; set; }
}
Running that code results in 3 tables like this:

How would I model data that is heirarchal and relational in a document-oriented database system like RavenDB?

Document oriented databases (particularly RavenDB) are really intriguing me, and I'm wanting to play around with them a bit. However as someone who is very used to relational mapping, I was trying to think of how to model data correctly in a document database.
Say I have a CRM with the following entities in my C# application (leaving out unneeded properties):
public class Company
{
public int Id { get; set; }
public IList<Contact> Contacts { get; set; }
public IList<Task> Tasks { get; set; }
}
public class Contact
{
public int Id { get; set; }
public Company Company { get; set; }
public IList<Task> Tasks { get; set; }
}
public class Task
{
public int Id { get; set; }
public Company Company { get; set; }
public Contact Contact { get; set; }
}
I was thinking of putting this all in a Company document, as contacts and tasks do not have a purpose out side of companies, and most of the time query for a task or contacts will also show information about the associated company.
The issue comes with Task entities. Say the business requires that a task is ALWAYS associated with a company but optionally also associated with a task.
In a relational model this is easy, as you just have a Tasks table and have the Company.Tasks relate to all tasks for the company, while Contact.Tasks only show the tasks for the specific Task.
For modeling this in a document database, I thought of the following three ideas:
Model Tasks as a separate document. This seems kind of anti-document db as most of the time you look at a company or contact you will want to see the list of tasks, thus having to perform joins over documents a lot.
Keep tasks that are not associated with a contact in the Company.Tasks list and put tasks assocaited with a contact in the list for each individual contacts. This unfortunately means that if you want to see all tasks for a company (which will probably be a lot) you have to combine all tasks for the company with all tasks for each individual contact. I also see this being complicated when you want to disassociate a task from a contact, as you have to move it from the contact to the company
Keep all tasks in the Company.Tasks list, and each contact has a list of id values for tasks it is associated with. This seems like a good approach except for having to manually take id values and having to make a sub-list of Task entities for a contact.
What is the recommended way to model this data in a document oriented database?
Use denormalized references:
http://ravendb.net/faq/denormalized-references
in essence you have a DenormalizedReference class:
public class DenormalizedReference<T> where T : INamedDocument
{
public string Id { get; set; }
public string Name { get; set; }
public static implicit operator DenormalizedReference<T> (T doc)
{
return new DenormalizedReference<T>
{
Id = doc.Id,
Name = doc.Name
}
}
}
your documents look like - i've implemented the INamedDocument interface - this can be whatever you need it to be though:
public class Company : INamedDocument
{
public string Name{get;set;}
public int Id { get; set; }
public IList<DenormalizedReference<Contact>> Contacts { get; set; }
public IList<DenormalizedReference<Task>> Tasks { get; set; }
}
public class Contact : INamedDocument
{
public string Name{get;set;}
public int Id { get; set; }
public DenormalizedReference<Company> Company { get; set; }
public IList<DenormalizedReference<Task>> Tasks { get; set; }
}
public class Task : INamedDocument
{
public string Name{get;set;}
public int Id { get; set; }
public DenormalizedReference<Company> Company { get; set; }
public DenormalizedReference<Contact> Contact { get; set; }
}
Now saving a Task works exactly as it did before:
var task = new Task{
Company = myCompany,
Contact = myContact
};
However pulling all this back will mean you're only going to get the denormalized reference for the child objects. To hydrate these I use an index:
public class Tasks_Hydrated : AbstractIndexCreationTask<Task>
{
public Tasks_Hydrated()
{
Map = docs => from doc in docs
select new
{
doc.Name
};
TransformResults = (db, docs) => from doc in docs
let Company = db.Load<Company>(doc.Company.Id)
let Contact = db.Load<Contact>(doc.Contact.Id)
select new
{
Contact,
Company,
doc.Id,
doc.Name
};
}
}
And using your index to retrieve the hydrated tasks is:
var tasks = from c in _session.Query<Projections.Task, Tasks_Hydrated>()
where c.Name == "taskmaster"
select c;
Which i think is quite clean :)
As a design conversation - the general rule is that if you ever need to load the child documents alone as in - not part of the parent document. Whether that be for editing or viewing - you should model it with it's own Id as it's own document. Using the method above makes this quite simple.
I'm new to document dbs as well...so with a grain of salt...
As a contrasting example...if you are on Twitter and you have a list of the people you follow, which contains a list of their tweets...you would not move their tweets into your twitter account in order to read them, and if you re-tweet, you would only have a copy, not the original.
So, in the same way, my opinion is that if Tasks belong to a company, then they stay within the Company. The Company is the Aggregate Root for Tasks. The Contacts can only hold references (ids) or copies of the Tasks and cannot modify them directly. If you have your contact hold a "copy" of the task, that's fine, but in order to modify the task (e.g. mark it complete) you would modify the task through its Aggregate Root (Company). Since a copy could quickly become outdated, it seems like you would only want a copy to exist while in memory and when saving the Contact, you would only save references to the Tasks.

Resources