After reading this good BLOG about adding custom data to UserProfile table, I wanted to change it the way that UserProfile table should store default data + another class where all additional info is stored.
After creating new project using Interenet application template, I have created two classes:
Student.cs
[Table("Student")]
public class Student
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public virtual int StudentId { get; set; }
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
public virtual int UserId { get; set; }
}
UserProfile.cs
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual int StudentId { get; set; }
public virtual Student Student { get; set; }
}
also, I've deleted the UserProfile definition from AccountModel.cs. My DB context class looks like this:
MvcLoginDb.cs
public class MvcLoginDb : DbContext
{
public MvcLoginDb()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Student> Students { get; set; }
}
again, I have deleted db context definition from AccountModel.cs.
Inside Package-Manager-Console I've written:
Enable-Migrations
and my Configuration.cs looks like this:
internal sealed class Configuration : DbMigrationsConfiguration<MvcLogin.Models.MvcLoginDb>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(MvcLogin.Models.MvcLoginDb context)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
if (!WebSecurity.UserExists("banana"))
WebSecurity.CreateUserAndAccount(
"banana",
"password",
new
{
Student = new Student { Name = "Asdf", Surname = "Ggjk" }
});
}
}
That was the idea of adding student data as creating a new class, but this approach is not working because after running Update-Database -Verbose I'm getting the error:
No mapping exists from object type MvcLogin.Models.Student to a known managed provider native type.
Can anyone expain why I'm getting this error, shoud I use a different approach for storing additional data in different table?
I struggled a lot with the same issue. The key is to get the relation between the UserProfile class and your Student class correct. It have to be a one-to-one relationship in order to work correct.
Here is my solution:
Person.cs
public class Person
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
/* and more fields here */
public virtual UserProfile UserProfile { get; set; }
}
UserProfile.cs
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual Person Person { get; set; }
}
MyDbContext.cs
public class MyDbContext : DbContext
{
public DbSet<Person> Person { get; set; }
public DbSet<UserProfile> UserProfile { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<UserProfile>()
.HasRequired(u => u.Person)
.WithOptional(p => p.UserProfile)
.Map(m => m.MapKey("PersonId"));
}
}
However, I also struggled with creating users with the WebSecurity.CreateUserAndAccount method. So I ended up creating my Person objects with Entity Framework and then create the user account with the membership provider method:
AccountRepository.cs
public Person CreatePerson(string name, string username) {
Person person = new Person { Name = name };
_dbContext.Person.add(person);
_dbContext.SaveChanges();
var membership = (SimpleMembershipProvider)Membership.Provider;
membership.CreateUserAndAccount(
model.UserName,
createRandomPassword(),
new Dictionary<string, object>{ {"PersonId" , person.Id}}
);
}
HenningJ, I'm searching for an answer to the same thing. Unfortunately (I'm sure you're aware) the problem with the way you did this is that there is no SQL transaction. Either your SaveChanges() or CreateUserAndAccount() could fail leaving your user database in an invalid state. You need to rollback if one fails.
I'm still searching for the final answer, but I'll post whatever solution I end up going with. Hoping to avoid having to write a custom membership provider (AGAIN!), but starting to think it would be faster.
Related
I have 3 entities: Person, User, and Location.
A Person can have multiple Locations
A User can have multiple Locations
My entities are set up as such:
public class Person
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual IList<Location>? Locations { get; set; }
}
public class PersonEntityTypeConfiguration : IEntityTypeConfiguration<Person>
{
public void Configure(EntityTypeBuilder<Person> builder)
{
builder
.HasMany(b => b.Locations)
.WithOne(b => b.Person)
.HasForeignKey(b => b.PersonId)
.IsRequired(false);
}
}
public class User
{
public Guid Id { get; set; }
public Guid? Username { get; set; }
public virtual IList<Location>? Locations { get; set; }
}
public class UserEntityTypeConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder
.HasMany(b => b.Locations)
.WithOne(b => b.User)
.HasForeignKey(b => b.UserId)
.IsRequired(false);
}
}
public class Location : UdbObject
{
public Guid Id { get; set; }
public string Description { get; set; }
[ForeignKey(nameof(Person))]
public Guid? PersonId { get; set; }
public virtual Person? Person { get; set; }
[ForeignKey(nameof(User))]
public Guid? UserId { get; set; }
public virtual User? User { get; set; }
}
Problem: I tried to insert a User into my SQL Server DB. This user has one Location object within its IList<Location>? Locations collection. I am getting the following error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Locations_Persons_PersonId".
Here is where it is going wrong:
Since Person.Id is a Guid? object, it automatically gets set to the equivalent of Guid.Empty before it is submitted to the DB. This causes the FK conflict, since the DB sees that there is no Person object in the DB with an Id set to the equivalent of Guid.Empty.
What I've tried: I saw that in previous version of EF Core, there is a .WithOptional() method that can be used in the Fluent API, but unfortunately this method is not recognized in EF Core 7. I tried to use the .IsRequired(false) method in the API, and it probably works from the DB standpoint, but my problem is that the GUID-based Id property is being set to Guid.Empty on the server before being passed to the DB, so .IsRequired(false) doesn't have the opportunity to do its job.
Am I missing something? Is there some other way to configure this?
Solution: I had a PersonDto that had a property public Guid Id { get; set; } instead of Guid? and it was being mapped back to the Person object with Guid.Empty loaded in it. Duh.
Just make them M2M relationships and the foreign keys will all be in bridge tables. eg
public class Location : UdbObject
{
public Guid Id { get; set; }
public string Description { get; set; }
public virtual ICollection<Person> Persons { get; } = new HashSet<Person>();
public virtual ICollection<User> Users { get; } = new HashSet<User>();
}
I've made a simple Entity Framework ASP Core Application that works but I do not know why:
I've made a context like this:
public class AstootContext : DbContext
{
public AstootContext(DbContextOptions<AstootContext> options)
: base(options)
{ }
public DbSet<Account> Accounts { get; set; }
public DbSet<User> Users { get; set; }
}
And I have two tables with models like this:
public class Account
{
public int Id { get; set; }
public string Username { get; set; }
public string PasswordHash { get; set; }
public DateTime Created { get; set; }
List<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public DateTime Birthday { get; set; }
public Account Account { get; set; }
}
The interesting thing is that when I run my application it actually can pick up the data. It just seems weird because I have not specified any table mapping.
I'm assuming this just automaps because the specified tables are the same name.
My questions are:
How do I specify Table explicit table mapping in case I do not want my model names to be exactly the same as the DB?
How do I specify Custom Column Mapping.
Is there anything special I have to specify for Primary/Foreign Keys
edit
To clarify
Say I had a table in the DB MyAccounts and I wanted to map that to an entity Accounts.
Say I had a column password and I wanted that to map to a POCO property PasswordHash
To specify the name of the database table, you can use an attribute or the fluent API:
Using Attributes:
[Table("MyAccountsTable")]
public class Account
{
public string PasswordHash { get; set; }
}
Using Fluent API:
public class YourContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Language>(entity => {
entity.ToTable("MyAccountsTable");
});
}
}
To name your columns manually, it's very similar and you can use an attribute or the fluent API:
Using Attributes:
public class Account
{
[Column("MyPasswordHashColumn")]
public string PasswordHash { get; set; }
}
Using Fluent API:
public class YourContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Language>(x => x
.ToTable("MyAccountsTable")
.Property(entity => entity.PasswordHash)
.HasColumnName("MyPasswordHashColumn")
);
}
}
I'm using EntityFramework with a link table and I have created a OnModelCreating that creates a link table called "RolePrivileges". But I have another dbcontext that uses the same database but with the relation the other way so I get this error:
Invalid object name 'dbo.PrivilegeRoles'
My Privilege class has a public virtual ICollection<Role> Roles { get; set; } property.
How can I tell EF that it's from RolePrivileges and not PrivilegeRoles?
UPDATE
I have added some code, this a big project and I am using repository pattern and all that. This is just to show the error
I have many projects, one for only generating the database that has all entities, migrations and all that.
in the dbcontext in this project I have
public class EasyhoursDbContext : IdentityDbContext<ApplicationUser>
{
...
public DbSet<Role> AccessRoles { get; set; }
public DbSet<Privilege> Privileges { get; set; }
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
modelBuilder.Entity<Role>()
.HasMany(role => role.Privileges)
.WithMany(p => p.AccessRoles)
.Map(ap =>
{
ap.MapLeftKey("RoleId");
ap.MapRightKey("PrivilegeId");
ap.ToTable("RolePrivileges");
});
...
}
}
public class Role
{
[Key]
public string Id { get; protected set; }
public string Name { get; protected set; }
public virtual List<Privilege> Privileges { get; private set; }
}
public class Privilege
{
[Key]
public string Id { get; set; }
...
public virtual ICollection<Role> AccessRoles { get; set; }
}
And then in another project I have a dbcontext that just contains this
public class RoleDbContext : DbContext
{
public RoleDbContext()
: base("DefaultConnection")
{
}
public DbSet<Role> AccessRoles { get; set; }
public DbSet<Privilege> Privileges { get; set; }
}
public class Privilege
{
public string Id { get; set; }
...
[JsonIgnore]
[IgnoreDataMember]
public virtual ICollection<Role> AccessRoles { get; set; }
}
public class Role
{
public string Id { get; protected set; }
public string Name { get; protected set; }
public List<Privilege> Privileges { get; set; } = new List<Privilege>();
}
And here's an example where I get the error:
var db = new RoleDbContext();
var role = db.AccessRoles.FirstOrDefault(r => true);
var privilege = db.Privileges.FirstOrDefault(p => true);
role.Privileges.Add(privilege);
db.Entry(role).State = EntityState.Modified;
db.SaveChanges();
On save changes I get the error:
Running this exact same code, but with EasyhoursDbContext instead it works fine
UPDATE
I updated the RoleDbContext now to contain the exact same rule for the onmodelcreation as EasyhoursDbContext.
But now I'm getting this error:
Violation of PRIMARY KEY constraint 'PK_dbo.RolePrivileges'. Cannot insert duplicate key in object 'dbo.RolePrivileges'. The duplicate key value is (03fd67b6-277f-43f6-b276-5bafbdbe55af, A657a693-0961-Role-b86b-381261aApply).\r\nThe statement has been terminated.
I wanna thank Steve Greene for helping me with finding the problem.
All I needed was to add OnModelCreation to my RolesDbContext
modelBuilder.Entity<Role>()
.HasMany(role => role.Privileges)
.WithMany(p => p.AccessRoles)
.Map(ap =>
{
ap.MapLeftKey("RoleId");
ap.MapRightKey("PrivilegeId");
ap.ToTable("RolePrivileges");
});
There are numerous questions in varying contexts regarding this error and so far I've been unable to find one that applies to my situation.
I have a many to many relationship I'm trying to establish with a 3rd table. It's basically a person to manager association. All managers are people but not all people are managers and also, every person has many managers and every manager has many people. So in the manager's table are PersonIds. In the people controller under edit is the option to associate people with managers. When clicking the available managers and clicking save, I get the error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.ManagerToEngineer_dbo.Manager_ManagerId". The conflict occurred in database "PROJECTNAME_16ce3f6a2a6c4ff0b1ce147d126984ba", table "dbo.Manager", column 'ManagerId'. The statement has been terminated.
The SaveChanges method results in this error:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(PersonViewModel person)
{
if (ModelState.IsValid)
{
//var MyPerson = db.people.Find(person.PersonId);
//MyPerson.FirstName = person.FirstName;
//MyPerson.LastName = person.LastName;
foreach (var item in db.ManagersToEngineers)
{
if (item.EngineerId == person.PersonId)
{
db.Entry(item).State = EntityState.Deleted;
}
}
foreach (var item in person.EngineerManagers)
{
if (item.Checked)
{
db.ManagersToEngineers.Add(new ManagerToEngineer() { EngineerId = person.PersonId, ManagerId = item.Id });
}
}
//db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
Relevant models:
public class ManagerToEngineer
{
[Key]
public int ManagerToEngineerId { get; set; }
[Required]
[ForeignKey("engineer")]
public int EngineerId { get; set; }
[Required]
[ForeignKey("manager")]
public int ManagerId { get; set; }
public virtual Person engineer { get; set; }
public virtual Manager manager { get; set; }
}
public class Manager
{
[Key]
public int ManagerId { get; set; }
[Required]
[ForeignKey("person")]
public int EngineerId { get; set; }
public virtual Person person { get; set; }
public ICollection<ManagerToEngineer> ManagersToEngineers { get; set; }
}
public class PersonViewModel
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[Display(Name = "Managers")]
public List<CheckBoxViewModel> EngineerManagers { get; set; }
}
public class CheckBoxViewModel
{
public int Id { get; set; }
public string FullName { get; set; }
public bool Checked { get; set; }
}
First, you should reconsider your models. The phrase:
All managers are people but not all people are managers
does not translate into a many-to-many relationship. A student has many courses and a course has many students is a many-to-many relationship. Your situation translated better as: every manager is a person which hints at inheritance rather than object composition. However, for learning purposes let's go ahead with your example.
The following code (I've changed naming a little bit, please don't use Engineer and Person interchangeably because it adds confusion):
public class Person
{
public Person()
{
Managers = new HashSet<Manager>();
}
[Key]
public int PersonId { get; set; }
public string Name { get; set; }
public virtual ICollection<Manager> Managers { get; set; }
}
public class Manager
{
public Manager()
{
Persons = new HashSet<Person>();
}
[Key]
public int ManagerId { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class CompanyContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Manager> Managers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.HasMany(p => p.Managers).WithMany(m => m.Persons)
.Map(t => t.MapLeftKey("PersonID")
.MapRightKey("ManagerID")
.ToTable("PersonToManager"));
}
}
You will notice why this is not a good design when performing an insertion:
static void Main(string[] args)
{
using (var db = new CompanyContext())
{
// add a person
var person = new Person() { Name = "Joe" };
db.Persons.Add(person);
// "make" him manager
var manager = new Manager();
manager.Persons.Add(person);
db.Managers.Add(manager);
db.SaveChanges();
}
Console.ReadKey();
}
The above code compiles and should run (but I could not test it because I'm having some SqlLocalDb issues). Notice that I don't need to explicitly create a class for the association tables, Entity Framework can deduce that by itself provided that your modelBuilder has the correct definition.
Please remember that the above code is only an example of a many-to-many relationship and not an actually good database design, but I've posted it anyway instead of just giving you a link to see the difference between your code and mine. Please follow the tutorial here for a better understanding and if this is a project meant to (at some point) be more than a learning exercise please read a few tutorials on how database design translates to code (e.g how to design an inheritance relationship in SQL).
You have a FK constraint on ManagersToEngineer so before CUDing anything on ManagersToEngineer, make sure the item referred on foreign tables Engineer and Manager (in your case, Manager) is valid.
To be more specific, does item.Id exist in your Manager table?
I have a POCO class that has two one-way unary relationships with another class, both classes share an ancestor. The names of the foreign keys in the generated schema do not reflect the property names. (Properties MainContact and FinancialContact give PersonId and PersonId1 field names).
How can I influence schema generation to generate database column names that match the property names?
The model looks like this:
The code looks like this:
public class CustomerContext: DbContext
{
public DbSet<Organisation> Organisations { get; set; }
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
DbDatabase.SetInitializer(new DropCreateDatabaseAlways<CustomerContext>());
}
}
public abstract class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Person : Customer
{
public string Email { get; set; }
}
public class Organisation : Customer
{
public Person FinancialContact { get; set; }
public Person MainContact { get; set; }
}
The schema looks like this:
Answer from druttka
druttka's answer below did the job and it's nice to know that it's a CTP5 bug that's behind this. EF also needs the cascade behaviour to be specified and I've used the fluent API to do this following the example in the link given by druttka. Some more good reading from Morteza Manavi here.
The code now is this:
public class CustomerContext : DbContext
{
public DbSet<Organisation> Organisations { get; set; }
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
DbDatabase.SetInitializer(new DropCreateDatabaseAlways<CustomerContext>());
builder.Entity<Organisation>()
.HasRequired(p => p.MainContact)
.WithMany()
.HasForeignKey(p => p.MainContactId)
.WillCascadeOnDelete(false);
builder.Entity<Organisation>()
.Property(p => p.MainContactId)
.HasColumnName("MainContact");
builder.Entity<Organisation>()
.HasRequired(p => p.FinancialContact)
.WithMany()
.HasForeignKey(p => p.FinancialContactId)
.WillCascadeOnDelete(false);
builder.Entity<Organisation>()
.Property(p => p.FinancialContactId)
.HasColumnName("FinancialContact");
}
}
public abstract class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Person : Customer
{
public string Email { get; set; }
}
public class Organisation : Customer
{
public Person FinancialContact { get; set; }
public int FinancialContactId { get; set; }
public Person MainContact { get; set; }
public int MainContactId { get; set; }
}
Which now gives the far more suitable database:
EF Code First uses, by default, convention over configuration. However, you can set explicit alternatives by overriding DbContent.OnModelCreating. Many examples here, courtesy of ScottGu.
EDIT
So in CTP5, MapSingleType went away as described here. The following works for simple string properties, but not for your Organisation to Person relationships. I'm curious and plan to keep looking at it, but in the meantime, maybe this will get your started or someone else can complete the answer.
public class Person : Customer
{
[Column(Name="EmailAddress")]
public string Email { get; set; }
}
EDIT 2
Ok, this gets it. Found the answer here. Disclaimer: I've only verified that the database schema is created as expected. I have not tested that seeding data or further CRUD operations work as expected.
public class Organisation : Customer
{
[Column(Name = "FinancialContact")]
public int? FinancialContactId { get; set; }
[ForeignKey("FinancialContactId")]
public Person FinancialContact { get; set; }
[Column(Name = "MainContact")]
public int? MainContactId { get; set; }
[ForeignKey("MainContactId")]
public Person MainContact { get; set; }
}