I'm using C#.NET, Entity Framework 4.1 and the code first approach. From that i have a bunch of entities and one entity is related to another entity. They have a primary key/foreign key relation.
I'm also using ViewModels to specify which attributes may be scaffolded and i was hoping that the two related entities would create a dropdown list. But that's not happening.
This is just an example of how my entity relation looks, to illustrate my problem.
UserType entity:
public class UserType
{
public int UserTypeID { get; set; }
public string Type { get; set; }
public virtual ICollection<User> Users { get; set; }
}
User entity:
public class User
{
public int UserID { get; set; }
public int UserTypeID
public string Username { get; set; }
public bool IsAdmin { get; set; }
public virtual UserType UserType { get; set; }
}
My ViewModel:
[JsonObject(MemberSerialization = MemberSerialization.OptOut, IsReference = false)]
[DataContract]
public class UserViewModel : BaseViewModel
{
[Key]
public int UserID { get; set; }
public int UserTypeID { get; set; }
[required]
public string Username { get; set; }
}
So the UserViewModel is scaffolded. And i want it to create a dropdown list for the UserType. But right now it only creates an input field for UserTypeID. How can i make it show a dropdown list with the values of UserTypes?
The scaffolder has no idea that UserTypeId in your view model relates to an actual UserType class, and therefore has no way of knwoing where to get the static list of user types from.
If you look at the example you referenced, it also includes a navigation property to the foreign key class.
Related
I am having issues trying to map two fields that are foreign keys into the same table. The use case is for a modifier and creator. My class already has the Ids, and then I wanted to add the full User object as virtual.
I am using a base class so that each of my tables have the same audit fields:
public class Entity
{
public long? ModifiedById { get; set; }
public long CreatedById { get; set; } = 1;
[ForeignKey("CreatedById")]
public virtual User CreatedByUser { get; set; }
[ForeignKey("ModifiedById")]
public virtual User ModifiedByUser { get; set; }
}
The child class is very simple:
public class CircleUserSubscription : Entity
{
[Required]
public long Id { get; set; }
public long SponsorUserId { get; set; }
[ForeignKey("SponsorUserId")]
public virtual User User { get; set; }
public long TestId { get; set; }
[ForeignKey("TestId")]
public virtual User Test { get; set; }
}
This is a standard junction table.
When I try to generate the migration, I am getting errors that I don't understand fully.
Unable to determine the relationship represented by navigation property 'CircleUserSubscription.User' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
I tried what this answer had, but the code is basically the same: https://entityframeworkcore.com/knowledge-base/54418186/ef-core-2-2---two-foreign-keys-to-same-table
An inverse property doesn't make sense since every table will have a reference to the user table.
For reference, here is the User entity:
public class User : Entity
{
public long Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I am hoping you all can help me out, TIA :)
EDIT: One thing to note, all of this worked fine when the entity class was as follows:
public class Entity
{
public long? ModifiedById { get; set; }
public long CreatedById { get; set; } = 1;
}
It was only after I added the entity that things went awry.
I am currently trying to model an object class with a one-to-many relationship with another object in Model-First MVC- a "Contact" object that includes, among other things, a list of (references to?) one or more "Interests", chosen upon creation of a Contact from the full database list of Interests. It is my understanding that one uses a ViewModel to accommodate such a relationship, but I think I am misunderstanding what does or does not go in the model vs the viewModel.
At present, I have a Contacts model:
public class Contact
{
public int Id { get; set; }
public string Email { get; set; }
public virtual List<Interest> Interests { get; set; }
public List<int> InterestIds { get; set; }
}
An Interest model:
public class Interest
{
public int Id { get; set; }
public string Name { get; set; }
}
A Contact viewModel:
public class ContactViewModel
{
public int Id { get; set; }
public string Email { get; set; }
public List<int> InterestIds { get; set; }
public List<InterestViewModel> Interests { get; set; }
}
And an Interest viewModel
public class ContactViewModel
{
public int Id { get; set; }
public string Email { get; set; }
public List<int> InterestIds { get; set; }
public List<InterestViewModel> Interests { get; set; }
}
But I'm getting conflicted accounts of what goes where.
In short, I intend the end result that I should be able to choose one or more Interests (preferably with a series of checkboxes) from the Create view to be stored in the new Contact, and then from the Index view be able to filter the table of Contacts by which Interests they do or do not have. I have logic planned out for most of that already, but for the time being how should I design my Models vs my ViewModels in order to best accommodate this vision?
your Contact entity has a logical error, you should remove InterestIds property from Contact entity and add a property with name ContactId to your Interest entity. it provides foreign key for contact(one to many relation from Contact to Interest). however if ViewModel is equals to the Model, so you don't need any ViewModel.
Note: for this question, I've simplified the models a bit.
Im working on an ASP.NET Core MVC application (with Entity Framework Core). I have three models. I have an ApplicationUser (that extends from IdentityUser), I have an Activity model and I have a 'SignUp' model that has a foreign key to an activity and a foreign key to ApplicationUser. At least, that's the idea. The thing is, Entity Framework recognices the Activity FK as a FK, but not the applicationuser. That just becomes another int in the database without any FK constraints.
I've tried a lot of things that I found on the internet, but I can't get it to work. This is what I currently have (shortened for clarity):
Activity:
public class Activity
{
public Activity()
{
SignUps = new HashSet<SignUp>();
}
public int ActivityID { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Naam")]
public string Name { get; set; }
public virtual ICollection<SignUp> SignUps { get; set; }
}
ApplicationUser:
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
Activities = new HashSet<Activity>();
}
[Required]
[Display(Name = "Voornaam")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Achternaam")]
public string LastName { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
}
SignUp:
public class SignUp
{
public int SignUpID { get; set; }
[Required]
public int ActivityID { get; set; }
[ForeignKey("ApplicationUser")]
public int ApplicationUserID { get; set; }
[Required]
public string Status { get; set; }
}
Note: I first did not have the [ForeignKey] attribute (like with activity), but it's there because I was trying to get it working.
As I said above, the Activity FK in SignUp works fine, the Applicationuser FK doesn't. Entity Framework just generates an int in the database without any FK constraints and when scaffolding CRUD pages, it's also not recognized as an FK.
I'm quite new to ASP.net MVC and EF and I have the feeling I'm overlooking something very simple. Facepalm moment incoming?
Thanks!
The Activity relationship works because you have navigation properties involved there. Activity has an ICollection<SignUp>, so EF is binding to ActivityID on SignUp to maintain that collection. There is nothing similar being done with ApplicationUser. You just have an property called ApplicationUserID; EF will not do anything with that because it has no idea it should.
Just add a navigation property and you'll be good:
[ForeignKey("ApplicationUser")]
public int ApplicationUserID { get; set; }
public ApplicationUser ApplicationUser { get; set; }
EDIT
Actually, after looking at your code a bit more, I think you may have just messed up with the ICollection<Activity> on ApplicationUser. There's no direct relationship between ApplicationUser and Activity. That relationship comes via SignUp, which is basically an M2M with a payload. As a result, you should have the following instead:
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
Activities = new HashSet<Activity>();
}
[Required]
[Display(Name = "Voornaam")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Achternaam")]
public string LastName { get; set; }
public virtual ICollection<SignUp> SignUps { get; set; }
}
Then, it would function exactly the same as your Activity relationship does currently.
I'm trying to implement inheritance using entity framework 6.0 and database first approach. OK, let's say I have a Person and an Organization entity like below:
// a simplified version of organization entity
public class Organization
{
public Guid ID { get; set; }
public string Nickname { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string OfficialName { get; set; }
public Guid CEOID { get; set; }
public DateTime? RegisterDate { get; set; }
}
// a simplified version of person entity
public class Person
{
public Guid ID { get; set; }
public string Nickname { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public Guid PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string NationalCode { get; set; }
public DateTime? BirthDate { get; set; }
}
I can create these two tables in database, but I want to use inheritance so the fields which is repeated in both Person and Organization could be in another base class like below:
public class Identity
{
// These fields are the common fields between Person and Organization
public Guid ID { get; set; }
public string Nickname { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
How can I achieve this in db-first approach?
One possible way is to use one table for each type called TPT (table-per-type), which I prefer to use. To achieve this, you define your tables like the model shown in the following picture:
Note that the relationships between child and base entity are one-to-one on their pk columns, and all common fields are moved to the base table. After creating your tables, right click on the models page in your visual studio, and select Update Model from Database..., and then in the add tab, select these 3 tables to add. At first you should see this model diagram, which needs to be changed a bit:
Do these steps for Person and Organization separately:
Right click on entity and select Properties
In the Base Type property select Identity
Select and then delete the association between this entity and Identity
Select and then Delete the PK (ID column) of this entity (Inherits from base entity)
After these steps save your model. Now your model should look like this:
Now compile your project and enjoy your life!
Additional resources:
Entity Framework Designer TPH Inheritance
public class Product
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public bool IsInStock { get; set; }
public string ImageUrl { get; set; }
public List<ProductOption> ProductOptions { get; set; }
public virtual Category Category { get; set; }
}
public class ProductOption
{
[Key]
public int Id { get; set; }
public string ProductOptionName { get; set; }
public string ProductOptionDescription { get; set; }
}
Now I know when your using Code First EF, so that the tables are created correctly. You need to do something like this.
modelBuilder.Entity<Product>().HasMany(p => p.ProductOptions).WithMany().Map(m =>
{
m.MapLeftKey("ProductId").MapRightKey("ProductOptionId").ToTable("SelectedProductOptionsInOrderedItem");
});
So....
Does this mean that if I do something like Product.ProductOptions I will be able to access all associated productoptions.
Is this the best way to set it up, or is there another way?
To enable lazy load and EF can create derived proxy types for your collection, that property should be declared this way:
public virtual ICollection<ProductOptions> ProductOptions { get; set; }
That should be enought. Other aspect is the mapping approach that you use. You choose fluent api, i prefer mapping by convention, but that is a matter of personal taste anyway.
Ok, Mapping by Conventions:
Is the ability of EF that from the name of entities and their properties along with their types, to map our model with the underlying data without providing any other information.
for example
public class Customer {
public long CustomerID {get; September;}
public string CustomerName {get; September;}
public Employee AssignedTo {get; September;}
}
With the previous model EF will map database with a table named Customer with:
. CustomerID bigint primary key column
. CustomerName nvarchar column
. Customer_EmployeeID foreign key to Employee table, with the datatype Corresponding to EmployeeID in that table.
You can read more Here