Unable to save data using entity frame work code first technique - c#

I am unable to save data using code first technique.
here is my context:
public virtual DbSet<Users> User { get; set; }
public virtual DbSet<Agency> Agencies { get; set; }
public virtual DbSet<ColdStorage> ColdStorages { get; set; }
public virtual DbSet<ShowRoom> ShowRooms { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
and my user model is here:
[Table("Users")]
public class Users
{
[Key]
public int UserId { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool isAgency { get; set; }
public bool isColdStorage { get; set; }
public string VerificationCode { get; set; }
public DateTime CreatedDate { get; set; }
// Reverse navigation
public virtual Agency Agency { get; set; }
public virtual ColdStorage ColdStorage { get; set; }
public virtual ShowRoom ShowRoom { get; set; }
public Users()
{
Agency = new Agency();
ColdStorage = new ColdStorage();
ShowRoom = new ShowRoom();
}
}
and my other model is here:
[Table("Agency")]
public class Agency
{
[Key]
public int AgencyId { get; set; }
[ForeignKey("Users")]
public int UserId { get; set; }
public string AgencyName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Description { get; set; }
public DateTime CreatedDate { get; set; }
[Required]
public virtual Users Users { get; set; }//Foriegn key
}
And the error is here:
Message":"An error has occurred.","ExceptionMessage":"One or more validation errors were detected during model generation:\r\n\r\nColdStorage_Users_Source: : Multiplicity is not valid in Role 'ColdStorage_Users_Source' in relationship 'ColdStorage_Users'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.\r\nShowRoom_Users_Source:

Just like the error says.
"because the Dependent Role properties are not the key properties, the
upper bound of the multiplicity of the Dependent Role must be '*'"
The "Dependent Role properties" here means, eg Agency.UserId, and the "multiplicity must be '*'" means Users must have a collection of Agencies. EG
public virtual ICollection<Agency> Agencies { get; } = new HashSet<Agency>();

Related

The member with identity 'PmData.SafetyRequirement_Assets' does not exist in the metadata collection.\r\nParameter name: identity

I am trying to update an record in my system. Everything on the model saves great, except any of my many to many type relationships on the form. When I get to those in my model it gives me the error. "The member with identity 'PmData.SafetyRequirement_Assets' does not exist in the metadata collection.\r\nParameter name: identity". I've read over some of the other answers but I do not have any triggers on my database, and I've gone through several changes in my model based on other suggestions and it doesn't seem to change anything. The project is in vNext.
Here is my first model
public partial class Asset : DataModel
{
[Required]
[StringLength(64)]
public string Name { get; set; }
[StringLength(256)]
public string Description { get; set; }
[StringLength(1024)]
public string SystemFunction { get; set; }
[StringLength(2048)]
public string Remarks { get; set; }
public bool IsSystem { get; set; }
public bool IsGrouping { get; set; }
[StringLength(128)]
public string FieldTag { get; set; }
[ForeignKey("Parent")]
public int? ParentId { get; set; }
[ForeignKey("Building")]
public int? BuildingId { get; set; }
public bool IsOperable { get; set; }
public bool IsAvailable { get; set; }
public virtual Asset Parent { get; set; }
public virtual Building Building { get; set; }
public virtual ICollection<Asset> Children { get; set; }
public virtual ICollection<DrawingReference> DrawingReferences { get; set; }
public virtual ICollection<SpecReference> SpecReferences { get; set; }
public virtual ICollection<SafetyRequirement> SafetyRequirements { get; set; }
public virtual ICollection<SupportSystem> SupportSystems { get; set; }
}
The model for one the other table with a many to many.
public partial class SafetyRequirement : DataModel
{
[StringLength(256)]
[Required]
public string Name { get; set; }
[StringLength(2048)]
public string SafetyFunction { get; set; }
[StringLength(2048)]
public string FunctionalRequirements { get; set; }
[StringLength(2048)]
public string SystemBoundary { get; set; }
[StringLength(255)]
public string Reference { get; set; }
[ForeignKey("QualityLevel")]
public int QualityLevelId { get; set; }
public virtual QualityLevel QualityLevel { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
}
The map for the joining table
modelBuilder.Entity<Asset>().HasMany(t => t.SafetyRequirements)
.WithMany(t => t.Assets)
.Map(m =>
{
m.MapRightKey("SafetyRequirementId");
m.MapLeftKey("AssetId");
m.ToTable("AssetSafetyRequirement");
});
Finally here's the area that it fails...
public virtual void SaveAsync(TEntity model)
{
Task.Run(() =>
{
using (
var dbContext =
(TContext)
Activator.CreateInstance(typeof (TContext),
ConfigOptions == null ? ConfigService.ConnectionString : ConfigOptions.ConnectionString))
{
var dbSet = dbContext.Set<TEntity>();
dbSet.Attach(model);
dbContext.Entry(model).State = EntityState.Modified;
dbContext.SaveChanges();
}
});
}
Any information or pointers would be greatly appreciated.
You're trying to use both Fluent API and Data Annotations to define the relationships between your tables. Remove one or the other.

SQL Exception trying to add an existing user when context.SaveChanges() on a different table

I am trying to add an entry to a table that holds a users browsing history information. However, when trying to save the addition an SqlException is thrown:
Cannot insert duplicate key row in object 'dbo.AspNetUsers' with
unique index 'UserNameIndex'. The duplicate key value is
(exampleUserName). The statement has been terminated.
A user is has many browsing histories but a browsing history can only be attached to one user so there is a user as part of the BrowsingHistory DataModel:
namespace DataModels
{
[Table("BrowsingHistory")]
public class BrowsingHistory
{
[Key]
public int BrowsingHistoryId { get; set; }
public int ProductId { get; set; }
public System.DateTime DateTime { get; set; }
public int DeviceId { get; set; }
public int UserId { get; set; }
public virtual AspNetUsers User { get; set; }
public virtual Device Device { get; set; }
public virtual Product Product { get; set; }
}
}
It is to note that I am using the Microsoft Identity classes for my authentication. The user class looks as follows:
namespace DataModels
{
using System;
using System.Collections.Generic;
[Table("AspNetUsers")]
public class AspNetUsers
{
public AspNetUsers()
{
BrowsingHistories = new HashSet<BrowsingHistory>();
Orders = new HashSet<Order>();
AspNetUserClaims = new HashSet<AspNetUserClaims>();
AspNetRoles = new HashSet<AspNetRoles>();
}
[Key]
public int Id { get; set; }
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTime? LockoutEndDateUtc { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public string UserName { get; set; }
public string HouseName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string Postcode { get; set; }
public string ContactNumber { get; set; }
public virtual ICollection<BrowsingHistory> BrowsingHistories { get; set; }
public virtual ICollection<Order> Orders { get; set; }
public virtual ShoppingCart ShoppingCart { get; set; }
public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual ICollection<AspNetRoles> AspNetRoles { get; set; }
}
}
The error occurs when trying to save the addition in the repository. On the _context.SaveChanges() line the method below.
public void CreateBrowsingHistoryEntry(BrowsingHistory bhe)
{
_context.BrowsingHistory.Add(bhe);
_context.SaveChanges();
}
Any help with this issue would be greatly appreciated.

Error: Unable to determine composite primary key ordering for type

I try to scaffold a controller for my ClientModel and it gives me an error called:
There was an error running the selected code generator: 'Unable to retrieve metadata for '/////.ClientModel'. Unable to determine composite primary key for ordering for type '/////.ClientModel'. Use the ColumnAttribute (link) or the HasKey method (link) to specify an order for composite primary keys.
I have three classes with all of them an One-To-One relationship with my Client class.
This is my ClientModel:
[Table("Client")]
public class ClientModel : PersonModel
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ClientId { get; set; }
[Required]
public string FirstName { get; set; }
public string Initials { get; set; }
[Required]
public string LastName { get; set; }
//... snip ...
[Required]
public long ClientIdentificationNumber { get; set; }
public virtual PassportDetailsModel Passport { get; set; }
public virtual MembershipClientValidationModel Membership { get; set; }
public virtual AccountClientModel Account { get; set; }
}
Passport class:
[Table("Passport")]
public class PassportDetailsModel
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid PassportDetailsId { get; set; }
[Required]
public string IssuingCountry { get; set; }
[Required]
public DateTime Issued { get; set; }
[Required]
public DateTime ExpirationDate { get; set; }
public virtual ClientModel Client { get; set; }
}
Account class:
[Table("AccountClient")]
public class AccountClientModel
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid AccountId { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
public virtual ClientModel Client { get; set; }
}
Membership class:
[Table("MembershipClientValidation")]
public class MembershipClientValidationModel
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid MembershipClientValidationId { get; set; }
public DateTime MembershipStartDate { get; set; }
public DateTime MembershipExpiryDate { get; set; }
public bool IsMembershipValid { get; set; }
[Required]
public virtual ClientModel Client { get; set; }
}
I don't understand how to fix the 'unable to retrieve metadata for <class> error. How do I link classes with composite keys?
21-09-2015
After reading some comments I deleted the
DatabaseGenerated.DatabaseOption.Identity
In my primary key's in the other classes (Passport, Membership and Account). So I added some properties to my ClientModel:
[ForeignKey("Passport")]
public Guid PassportDetailsId { get; set; }
public virtual PassportDetailsModel Passport { get; set; }
[ForeignKey("Membership")]
public Guid MembershipClientValidationId { get; set; }
public virtual MembershipClientValidationModel Membership { get; set; }
[ForeignKey("Account")]
public Guid AccountClientId { get; set; }
public virtual AccountClientModel Account { get; set; }
It still gives me the same error as it gave me above.
I fixed this problem by doing some simple changes.
I removed my three [ForeignKey("//")] in my ClientModel. I added the Foreign keys to my Primary keys in my other 3 models with reference to my ClientModel.
What I did next was the major fix. I added [Required] above the public ClientModel Client { get; set; } in my other three models.
Now it works and scaffolds my ClientController without error(s).
So to show an example of one of the other three models:
public class MembershipClientValidationModel
{
[Key]
[ForeignKey("Client")]
public Guid ClientId { get; set; }
public DateTime MembershipStartDate { get; set; }
public DateTime MembershipExpiryDate { get; set; }
public bool IsMembershipValid { get; set; }
[Required]
public virtual ClientModel Client { get; set; }
}
It just works.

How to ignore an inner nested object when using AutoMapper

Hello I have the classes:
Class User
public class User
{
public Int64 Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Profile Profile { get; set; } //EF one to one
}
Class Profile
public class Profile
{
public Int64 Id { get; set; }
public string Skype { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual User User { get; set; } //This is because EF Mappings
}
Class User DTO
public class UserDTO
{
public string Name { get; set; }
public string Email { get; set; }
public Profile Profile { get; set; }
}
I did the configurations to Map User to UserDTO
Mapper.CreateMap<User, UserDTO>();
I need to have the Profile.User because of the Entity Framework One to One Relationship but I don't want the Profile.User to be shown in the Mapping.
How can I ignore the Profile.User?
You could use a UserProfileDTO class that omits User
public class UserProfileDTO
{
public string Skype { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public ICollection<AddressDTO> Addresses { get; set; }
}
public class UserDTO
{
public string Name { get; set; }
public string Email { get; set; }
public UserProfileDTO Profile { get; set; }
}
Mapper.CreateMap<User, UserDTO>();
Mapper.CreateMap<Profile, UserProfileDTO>();

Solution for IEnumerable.Contains() in entity framework linq queries

I am been using entity framework for my application. Unfortunately I can't make expressions like this one in entity framework:
List<MyUser> users = (from user in database.MyUsers
join app in database.MyApplications
on user.ApplicationId equals app.Id
where app.Name == this._applicationName
&& user.MyRoles.Contains(existingRole)
select user).ToList();
Any other approaches to this one. I don't want to change my database or my models. In my case the relationship between MyUser and MyRole is many to many with a glue table.
This is how the MyUser class is declared:
public partial class MyUser
{
public MyUser()
{
this.MyApiTokens = new HashSet<MyApiToken>();
this.MyLandingPages = new HashSet<MyLandingPage>();
this.MyPresentations = new HashSet<MyPresentation>();
this.MySlides = new HashSet<MySlide>();
this.MyUserSettings = new HashSet<MyUserSetting>();
this.MyRoles = new HashSet<MyRole>();
}
public System.Guid Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string EmailAddress { get; set; }
public int ApplicationId { get; set; }
public System.DateTime CreateDate { get; set; }
public System.DateTime LastActivityDate { get; set; }
public System.DateTime LastLockoutDate { get; set; }
public System.DateTime LastLoginDate { get; set; }
public System.DateTime LastPasswordChangedDate { get; set; }
public string PasswordQuestion { get; set; }
public string PasswordAnswer { get; set; }
public string Comment { get; set; }
public bool IsLocked { get; set; }
public bool IsApproved { get; set; }
public virtual ICollection<MyApiToken> MyApiTokens { get; set; }
public virtual MyApplication MyApplication { get; set; }
public virtual ICollection<MyLandingPage> MyLandingPages { get; set; }
public virtual ICollection<MyPresentation> MyPresentations { get; set; }
public virtual ICollection<MySlide> MySlides { get; set; }
public virtual ICollection<MyUserSetting> MyUserSettings { get; set; }
public virtual ICollection<MyRole> MyRoles { get; set; }
}
Assuming MyRoles is an association property in the entity model (i.e. maps to a table), you may want to make the matching explicit on the primary key of the role object. For example:
user.MyRoles.Any(r => r.RoleId == existingRole.RoleId)
There isn't enough information in your question to give an exact answer, though.

Categories