I currently have a class:
[Table("TitleCategoryMovie", Schema = "dbo")]
public class TitleCategoryMovie
{
[Key]
public int Id { get; set; }
public string CreatedBy { get; set; }
public DateTime CreateDatetime { get; set; }
public string UpdatedBy { get; set; }
public DateTime UpdateDatetime { get; set; }
[ForeignKey("Id")]
public virtual Title Title { get; set; }
}
There is a table called Title. And every TitleCategoryMovie has a corresponding Title, although not every Title has a TitleCategoryMovie. The column Id for Title is also the same for TitleCategoryMovie. But when I add both entity classes to my DbContext, it gives me an error:
Multiplicity is not valid in Role 'TitleCategoryMovieStandalone_Title_Source' in relationship 'TitleCategoryMovieStandalone_Title'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be �1�.
I've already defined my column Id in TitleCategoryMovie as a primary key and a foreign key to Title, so I don't know why it's giving me this error. What's wrong?
You should also add a TitleId field and use that one as the parameter in the [ForeignKey] attribute:
[Table("TitleCategoryMovie", Schema = "dbo")]
public class TitleCategoryMovie
{
[Key]
public int Id { get; set; }
public string CreatedBy { get; set; }
public DateTime CreateDatetime { get; set; }
public string UpdatedBy { get; set; }
public DateTime UpdateDatetime { get; set; }
public int TitleId { get; set; }
[ForeignKey("TitleId")]
public virtual Title Title { get; set; }
}
Update after comments
I believe something like this should work:
[Table("TitleCategoryMovie", Schema = "dbo")]
public class TitleCategoryMovie
{
[Key]
public int Id { get; set; }
public string CreatedBy { get; set; }
public DateTime CreateDatetime { get; set; }
public string UpdatedBy { get; set; }
public DateTime UpdateDatetime { get; set; }
public virtual Title Title { get; set; }
}
[Table("Title", Schema = "dbo")]
public class Title
{
[Key]
[ForeignKey("TitleCategoryMovie")]
public int Id { get; set; }
public string Something { get; set; }
public virtual TitleCategoryMovie TitleCategoryMovie { get; set; }
}
You should put the [ForeignKey] attribute in the class that you consider to be the child of the relationship.
I was mapping two entities to one table. Apparently this isn't allowed in Entity Framework, so that was why I was getting that error.
Related
I have the following code:
Class
public partial class Emp_Details
{
public long Emp_ID { get; set; }
public string Emp_Title { get; set; }
public string Emp_Name { get; set; }
public string Emp_LastName { get; set; }
public string Emp_Status{ get; set; }
}
entity Code
using (LoginEntities dbcontext = new LoginEntities())
{
Emp_Details emp = dbcontext.Emp_Details.Single(i => i.Emp_ID == empid);
emp.Emp_Status = status;
dbcontext.SaveChanges();
}
I am getting the exception
System.InvalidOperationException: The property 'Emp_Status' is part of the object's key information and cannot be modified.
I have searched for a solution and found 2 possible fixes:
1)Set a primary Key. If there is no primary key on a table, it will simply select the non-nullable columns as a concatenated primary key and the entity will be read/only.
2) Use a direct update such as:
dbcontext.Database.ExecuteSqlCommand(
"UPDATE Emp_Details SET [Emp_Status] = {0} WHERE [Emp_ID] = {1}", status, empid);
Is there any way to do this update without having to touch the database, set primary keys, or mess with the class file?
Your entity doesn't have a primary key. So:
public partial class Emp_Details {
public long Id { get; set; } // pay attention to this
public string Emp_Title { get; set; }
public string Emp_Name { get; set; }
public string Emp_LastName { get; set; }
public string Emp_Status{ get; set; }
}
Or
public partial class Emp_Details {
public long Emp_DetailsId { get; set; } // pay attention to this
public string Emp_Title { get; set; }
public string Emp_Name { get; set; }
public string Emp_LastName { get; set; }
public string Emp_Status{ get; set; }
}
Or
public partial class Emp_Details {
[Key] // pay attention to this
public long Emp_ID { get; set; }
public string Emp_Title { get; set; }
public string Emp_Name { get; set; }
public string Emp_LastName { get; set; }
public string Emp_Status{ get; set; }
}
Each of above changes, will solve the problem.
I have two class (tables)
Person { id(primary key) , code, name, address, ...}
Order {id(primary key) , order_number, customer, create_date, description, ...}
I want to create relationship between Person.code and Order.customer (one two many).
How can I create that relationship in EF code first.
OK it has an easy solution
[Table("Person")]
public partial class Person
{
public long ID { get; set; }
[Key()]
[StringLength(10)]
public string code { get; set; }
[StringLength(100)]
public string name { get; set; }
[Column(TypeName = "text")]
public string address{ get; set; }
public ICollection<Order> Orders { get; set; }
}
and for order
[Table("Order")]
public partial class Order
{
public long ID { get; set; }
public int order_number { get; set; }
[StringLength(10)]
public string customer { get; set; }
[Column(TypeName = "text")]
public string description { get; set; }
//...
[ForeignKey("customer")]
public Library Person { get; set; }
}
I will create a new question about specification various composite keys per each navigation property.
I am issuing a very strange scenario using Code first with existing database and asp.net identity entity framework. I have a simple userprofile model
[Table("CSUserProfile")]
public partial class UserProfile
{
[Key]
public string Id { get; set; }
[Required]
[Display(Name = "FirstName")]
public string FirstName { get; set; }
[Required]
[Display(Name = "LastName")]
public string LastName { get; set; }
[Required]
[Display(Name = "Phone")]
public string Phone { get; set; }
[Required]
public string Email { get; set; }
[Required]
[Display(Name = "Location")]
public string Location { get; set; }
[Required]
[Display(Name = "HomeTown")]
public string Hometown { get; set; }
public byte[] BlobData { get; set; }
[ForeignKey("fPersonLinkGID")]
public virtual List<ProfilePic> ProfilePic { get; set; }
}
and an image profile pic
[Table("CSProfilePic")]
public partial class ProfilePic
{
[Key]
public Guid? GID { get; set; }
public string fPersonLinkGID { get; set; }
public byte[] BlobData { get; set; }
}
the foreign key is the fPersonLinkGID. everything works fine but my problem is that if i want an one-to-one relation between the userprofile and the image like this
public virtual ProfilePic ProfilePic { get; set; }
(which is the correct scenario) I am getting this strange exception :
The ForeignKeyAttribute on property 'ProfilePic' on type 'eUni.Model.Application.UserProfile' is not valid. The foreign key name 'fPersonLinkGID' was not found on the dependent type 'eUni.Model.Application.UserProfile'. The Name value should be a comma separated list of foreign key property names.
I can not understand why I am getting that exception
You could read this answer. It introduces how to configure one to one relationship by HasRequired and WithOptional.
As for me, I will create one to one relationship by following way.
public class Store {
[Key]
public long Id { get; set; }
public virtual Item TheItem { get; set; }
// ....
}
public class Item {
// It is FK, and also PK.
[Key, ForeignKey("TheStore")]
public long Id { get; set; }
// The same string in the ForeignKey attribute. Ex: ForeignKey("TheStore")
public virtual Store TheStore { get; set; }
// ....
}
For all classes in my Entity I have a base class named CommonFields
public class CommonFields
{
public int Status { get; set; }
public DateTime CreatedOn { get; set; }
public int CreaedBy { get; set; }
public DateTime ModifiedOn { get; set; }
public int ModifiedBy { get; set; }
}
And, for eg. I have two classes like
public class Employee : CommonFields
{
public int Id { get; set; }
public string Name { get; set; }
//Other properties
}
public class User : CommonFields
{
public int Id { get; set; }
public string Name { get; set; }
//Other properties
}
How can I set relation from CreatedBy & ModifiedBy to User table. I just need only one directional mapping (I don't want FK to be created in my User Table).
I need to get User information when I write objEmployee.CreatedUser
Thanks.
I am new to EF and am having trouble figuring how to set up relationship between my main table Investors, with contact information, and a table Notes which can have many notes per investor. Here are the models:
public class Investor
{
public int Id { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Cell { get; set; }
public string Fax { get; set; }
[Display(Name="Address 1")]
public string Address1 { get; set; }
[Display(Name = "Address 2")]
public string Address2 { get; set; }
public string City { get; set; }
[StringLength(2, ErrorMessage = "State must be 2 characters")]
public string State { get; set; }
public string Zip { get; set; }
public string ContactTableId { get; set; }
[ForeignKey("ContactTableId, ContactId")]
public virtual List<Note> Notes { get; set; }
}
public class Note
{
[Key]
[Column(Order = 0)]
public string ContactTableId { get; set; }
[Key]
[Column(Order = 1)]
public int? ContactId { get; set; }
public string note { get; set; }
public DateTime? DateCreated { get; set; }
}
My attempt as setting this up, as above, generated the error 'The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.' on the statement:
public ActionResult Index()
{
return View(db.Investors.ToList());
}
in the controller. How do I set this up to make it pull the Notes automagically.
The foreign key is not "ContactTableId, ContactId", it is the single field Investor_Id in table Note (or Notes). EF thinks you try to map the single key to two field and coins this somewhat elusive exception message. But just remove the ForeignKey attribute and EF will use the foreign key field in Note.