mvc application not adding newly created model to database - sql

I recently added a new model to my MVC application that looks like this
namespace PIC_Program_1._0.Models
{
public class DisposalOrder
{
[Key]
[Display(Name = "DO ID")]
public int ID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Date Created")]
public DateTime DateCreated { get; set; }
[DefaultValue("Pending")]
public DOStatus Status { get; private set; }
public decimal disposed { get; set; }
public string Details { get; set; }
public virtual ICollection<DODetails> DisposalDetails { get; set; }
public virtual Site sites { get; set; }
public virtual Component Components { get; set; }
public virtual Item Items { get; set; }
public virtual Part Parts { get; set; }
public bool deleted { get; set; }
public bool hasBeenReleased { get; set; }
public string Notes { get; private set; }
}
}
But when I run a add-migration and an update database, nothing is changed.
I created the model by right-clicking my Models folder, then typing "Add class" so it should be setup the right way. I'm stumped as to why it's not adding?

Related

How do I extend IdentityUser class in DbFirst approach?

I have a table named User in my database. I also have a .net core project where authentication is built-in. I want to connect to my database and after scaffolding reveals my User class, I want it to inherit from IdentityUser.
After scaffolding went well, I tried to inherit from IdentityUser.
public partial class User :IdentityUser<int>
{
public int Id { get; set; }
public string Country { get; set; }
public string PersonalId { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public DateTime BirthDate { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public byte[] Idimage { get; set; }
public bool? EmailConfirmed { get; set; }
public bool? Smsconfirmed { get; set; }
}
I can not see Identity Fields like PasswordHash, PhoneNumberConfirmed and so on, in my database. Specifically, in User table.

Asp Core Multiple Entity Relationships

I am working on modeling a Contact Info Structure and haven't quite figured out how the relationships should be coded with EF Core. I am fairly new to using EF for data access layer.
I want to have a contact model which can contain Website, Phonenumbers, Emails, or Social Info. Then the contact info will be added to several different models. Any suggestions would be helpful, I am not sure how code this One to many with many table relationship or if it is even possible using EF.
Models so far
public class Contact
{
public String Id { get; set; }
public Int32 ContactType { get; set; } //Enum for Website, Phonenumbers, Emails, or Social
public String RecId { get; set; } //FK to multiple Models
public String RecType { get; set; }//Value for which model the RecID is for
public String Name { get; set; }
public String Value { get; set; }
}
public class ContactInfo
{
public virtual IList<Contact> Website { get; set; }
public virtual IList<Contact> PhoneNumbers { get; set; }
public virtual IList<Contact> Emails { get; set; }
public virtual IList<Contact> Socials { get; set; }
}
//Example of models to use the contact model
public class Company
{
....
pubic ContactInfo ContactInfo { get; set;}
}
public class Client
{
....
pubic ContactInfo ContactInfo { get; set;}
}
If I understand your question correctly, then you could use following code sample, but it is not exactly what you are trying to achieve. This may give you some understanding what you need to do with EF.
public class Contact
{
public String Id { get; set; }
public ContactType ContactType { get; set; } //Enum for Website, Phonenumbers, Emails, or Social
public String RecId { get; set; } //FK to multiple Models (This can't be the FK to multiple table as it should be FK for one table so that FK for Company would be CompanyId, FK for the Client should ClientId)
public String RecType { get; set; }//Value for which model the RecID is for (This need to rethink as it may not needed.)
public String Name { get; set; }
public String Value { get; set; }
// One to Many Relationship
public string CompanyId? { get; set; }
public string ClientId? { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class Company
{
public String Id { get; set; }
// Other properties
// One to Many Relationship
public ICollection<Contact> Contacts { get; set; }
}
public class Client
{
public String Id { get; set; }
// Other properties
// One to Many Relationship
public ICollection<Contact> Contacts { get; set; }
}
/* Db context */
public class YourDbContext : DbContext
{
public YourDbContext(DbContextOptions<YourDbContext> options)
: base(options)
{
}
public virtual DbSet<Contact> Contacts { get; set; }
public virtual DbSet<Company> Companies { get; set; }
public virtual DbSet<Client> Clients { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Contact>().HasKey(t => t.Id);
modelBuilder.Entity<Company>().HasKey(t => t.Id);
modelBuilder.Entity<Company>().HasMany(c => c.Contacts).WithOne(c => c.Company).HasForeignKey(k => k.CompanyId);
modelBuilder.Entity<Client>().HasKey(t => t.Id);
modelBuilder.Entity<Client>().HasMany(t => t.Contacts).WithOne(c =>c.Client).HasForeignKey(k => k.ClientId);
}
}
/* Db context - Endd */
public enum ContactType
{
Website,
PhoneNumbers,
Emails,
Social
}
Let me know if you need anymore information.
With the help from DSR, this is the solution I have (untested).
public class Company
{
public String Id { get; set; }
public String Name { get; set; }
public ICollection<ContactPhone> PhoneNumbers { get; set; }
public ICollection<ContactEmail> ContactEmail { get; set; }
public ICollection<ContactWebsite> ContactWebsite { get; set; }
public ICollection<ContactSocial> ContactSocial { get; set; }
}
public class Client
{
public String Id { get; set; }
public String Name { get; set; }
public ICollection<ContactPhone> PhoneNumbers { get; set; }
public ICollection<ContactEmail> ContactEmail { get; set; }
public ICollection<ContactWebsite> ContactWebsite { get; set; }
public ICollection<ContactSocial> ContactSocial { get; set; }
}
public class ContactWebsite
{
public String Id { get; set; }
public String Url { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class ContactPhone
{
public String Id { get; set; }
public String Type { get; set; }
public String Number { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class ContactEmail
{
public String Id { get; set; }
public String Category { get; set; }
public String Email { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class ContactSocial
{
public String Id { get; set; }
public String Site { get; set; }
public String Handle { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}

How to show a file in a Details view in asp.NET MVC4 web application?

I am working on an asp.NET MVC4 web application. I have a create view with Upload file (CV). I succeeded to upload the file correctly but I need to show it (the CV) in the Details view and i don't know how to do it.
Here's the class :
public class Consultant
{
public int Id { get; set; }
public int CIN { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public string RefCommercial { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public string DateNaissance { get; set; }
public string Fonction { get; set; }
public string Tel { get; set; }
public string Experience { get; set; }
public string Email { get; set; }
public string CVurl { get; set; }
The attribute CVurl contains the file's name.
Now i want to show the file in the Details view of a consultant.
Any help? Thanks

Error: Multiplicity Constraint Violated on db.SaveChanges?

I a am new to entity framework. I have created project with Entity Model. When i am trying to add item in to the table it gives error "Multiplicity Constraint violated" Any idea?
ID From Table MAPPER is FK in MAPS table.
In Code Behind:
private Mapper mMapper;
if (mMapper.Id == 0)
db.Mappers.Add(mMapper);
db.SaveChanges();
public partial class Mapper
{
public Mapper()
{
this.Maps = new HashSet<Map>();
}
public int Id { get; set; }
public string Name { get; set; }
public string RecordDelimiter { get; set; }
public string ValueDelimiter { get; set; }
public Nullable<int> ColumnDescriptionRow { get; set; }
public Nullable<int> FirstRowOfData { get; set; }
public Nullable<bool> IgnoreThisRecord { get; set; }
public Nullable<bool> ExplodePartQuantity { get; set; }
public Nullable<bool> IgnoreImportingErrors { get; set; }
public Nullable<int> AngleMappingLeadingEdgeId { get; set; }
public Nullable<int> AngleMappingTrailingEdgeId { get; set; }
public string ValueWrapper { get; set; }
public string ValueWrapperDelimiter { get; set; }
public virtual AngleMapping AngleMapping { get; set; }
public virtual AngleMapping AngleMapping1 { get; set; }
public virtual ICollection<Map> Maps { get; set; }
}
Help Appreciated!

Custom simple membership provider adding row

I have a custom SimpleMembershipProvider and i have defined all tables that the SimpleMembershipProvider requires, like webpages_Membership:
[Table("webpages_Membership")]
public class Membership
{
public Membership()
{
Roles = new List<Role>();
OAuthMemberships = new List<OAuthMembership>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
public DateTime? CreateDate { get; set; }
[StringLength(128)]
public string ConfirmationToken { get; set; }
public bool? IsConfirmed { get; set; }
public DateTime? LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
[Required, StringLength(128)]
public string Password { get; set; }
public DateTime? PasswordChangedDate { get; set; }
[Required, StringLength(128)]
public string PasswordSalt { get; set; }
[StringLength(128)]
public string PasswordVerificationToken { get; set; }
public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
public ICollection<Role> Roles { get; set; }
[ForeignKey("UserId")]
public ICollection<OAuthMembership> OAuthMemberships { get; set; }
}
But how do i add a record in this table within the Seed() method in the configuration.cs file that migrations creates when enabled?
Assuming you have the custom membership provider correctly configured in the web.config you should be able to add a record in the Seed method by doing this.
var membership = (MyMembershipNamespace.SimpleMembershipProvider)Membership.Provider;
membership.CreateUserAndAccount("test", "password");