cant create a relationship between application user table and my tables - entity

I'm using entity framework code first. The below keeps kicking out the unable to determine the principal end of an association. Basically I can't figure out how to get one to many relationships between the auto generated identity table and my own tables with code first.
public class applicationUser : Identity
{
public User user {get;set;}
}
//and then a dependant class called User
public class User
{
public in Id {get;set;}
public string Name {get;set;}
[ForeignKey("Identity")]
public string IdentityId {get;set;}
public ApplicationUser Identity {get;set;}
public string UserPicLocation {get;set;}
}

You're using [ForeignKey()] wrong. The value of [ForeignKey()] should be the name of the property that contains the key of the referenced object.
You should also mark the property that contains the key in the referenced object as the private key with [Key]
Try this:
public class applicationUser : Identity
{
public int UserId {get; set;}
[ForeignKey("UserId")]
public User user {get;set;}
}
and then another class called User
public class User
{
[Key]
public int Id {get;set;}
public string Name {get;set;}
public string IdentityId {get;set;}
public ApplicationUser Identity {get;set;}
public string UserPicLocation {get;set;}
}

I fixed it by setting the primary key as both the primary and foreign key in the dependant table

Related

Entity Framework Core TPH Inheritance 2 sub Classes related to Other Model

I am devoloping an Asp.Net Core 3 App with Entity Framework Core.
I have a problem with Inheritance and I am not sure how to accomplish what I need.
My Models:
public abstract class Person
{
public int Id {get; set;}
public string name {get; set;}
}
public class Renter: Person
{
public string address{get; set;}
public ICollection<Invoice> Invoices{get;set;}
}
public class UserProfile: Person
{
public string BankAccount {get;set;}
public ICollection<Invoice> Invoices{get;set;}
}
public class Invoice
{
public DateTime Date {get;set;}
public Renter Renter {get; set;}
[Required]
public int RenterId {get; set;}
public UserProfile UserProfile{get; set;}
[Required]
public int UserProfileId {get; set;}
}
Right Now I get a Database Exception: Foreign Key Constraint Fails : RenterId References Person.Id .
Can I Use Fluent Api to manually configure my Setup?
The problem is that the Invoice needs both a Renter and a UserProfile, but Person.Id is the Primary Key for both Renter and UserProfile.
Any help is gratefully appreciated.
Yes, you can use fluent API. Assuming that you are using Table per Type (TPT):
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Renter>().ToTable("Renter");
modelBuilder.Entity<UserProfile>().ToTable("UserProfile");
}
See: https://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx

Microsoft.AspNet.Identity - What data should be stored as an identity claim vs another database table

What would be best practice to determine what I should store in the identity claim object versus in a separate table. For example, let's say that each user needs to have access to multiple sites. See object below:
public class Site
{
public int SiteID { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}
Should i store the Site object as a Serialized Claim or should I create database table for it, as well make the Site object a property of the Application User class of Microsoft.AspNet.Identity framework.
public class ApplicationUser : IdentityUser
{
public Site[] Sites {get; set;}
}

Several unique constrains on a single document in RavenDb

Let's say I have a class:
public class Person
{
public string Name{get;set;}
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
}
If I want to make the email unique I will use the unique constraint bundle.
But I want to make both the googleId and the facebookId properties as a single unique constraint side by side with the email constraint (while non of them is the id).
Is it possible?
Use the UniqueConstraints bundle:
public class Person
{
public string Name {get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string GoogleAndFacebookIds { get;set; }
}
Just make sure you update GoogleAndFacebookIds everytime you update either GoogleId or FacebookId. I was doing this so much I ended up using a simple interface on all my classes that did this sort of thing:
public interface ICombinedConstraints
{
string UniqueId { get; set; }
void UpdateConstraints();
}
So,
public class Person : ICombinedConstraints
{
public string Name{get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string UniqueId { get; set; }
public void UpdateConstraints()
{
UniqueId = GoogleId + FacebookId;
}
}
You cannot do that. Raven doesn't provide any possibility to enforce unique constraints on the property. If you want to do that you need separate sets of documents (see link below)
Unique constraints in Raven
Update:
It seems that you can also try to use bundles to implement unique constraints in the object.
Bundle: Unique constraints

Entity Framework Code First - using UserId key of Memberships/Users table as Foreignkey in a custom table

I am trying to create a SQL table to include additional user information.
I want it to be created by Migrations in VS2012 (package console => update-database).
I see that there are two tables using UserId column as key: Memberships and Users tables.
I am trying to define the following class and map it to SQL table via Migrator:
[Key]
[Column(Order = 0)]
[ForeignKey("User")]
public Guid **UserId** { get; set; }
[Key]
[Column(Order = 1)]
[ForeignKey("Category")]
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
**public virtual User User { get; set; }**
Although it is obvious in the Users SQL table that UserId column is the key, I get this error message:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'User' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Users' is based on type 'User' that has no keys defined.
What I am missing here? Might be that Microsoft.VisualBasic.ApplicationServices.User / System.Web.Security.MembershipUser classes weren't necessarily mapped to the tables this way and vise versa, and therefore the UserId property is not declared is Key dataannotation?
I am open for other solutions for this problem.
Big Thanks!
I am currently using asp.net mvc4 with Azure db.
When I want to use UserId in other tables from UserProfile. Notice in AccountModels there is class UserProfile:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
Now, let's say your category is created by certain user, you can link category entry to user in following way.
[Table("Category")]
public class Category
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CategoryId { get; set; }
public UserProfile CreatorUserId { get; set; }
}
Otherwise, you can always use UserProfile as model.

Problem Understanding Fluent nHibernate Automapping and Relationship

I am a bit new to Fluent nHibernate and ran into a scenario with my schema I'm not sure how to address.
Say I have two tables:
Track
TrackId
UserId
Name
Users
UserId
Name
Now, what I want to do is have the ability to access the related User object by track. For example:
var track = repo.GetById(1);
var userName = track.User.Name;
How can I get nHibernate to automap this new custom User property?
Here you go:
public class Track
{
public virtual int Id {get;set;}
public virtual string Name {get;set;}
public virtual User User {get;set;}
}
public class User
{
public virtual int Id {get;set;}
public virtual string Name {get;set;}
}
// Usage
var track = repo.GetById(1);
var username = track.User.Name;
More information can be found here.