I will show how to create a sample asp.net mvc 5 project. First I will use visual studio 2013 web express for creating asp.net mvc 5 project. I create a new project by File> New Project > ASP.NET Web Application > MVC project. Now according to our requirements I create model schema customer.
public class Customer { [Key] public int CustomerId { get; set; } [Required] [StringLength(256, ErrorMessage = "Company cannot be longer than 256 characters.")] [Display(Name = "Company")] public string Company { get; set; } [Required] [StringLength(256, ErrorMessage = "First Name cannot be longer than 256 characters.")] [Display(Name = "First Name")] public string Name { get; set; } [Required] [StringLength(256, ErrorMessage = "Last Name cannot be longer than 256 characters.")] [Display(Name = "Last Name")] public string LastName { get; set; } [Required] [StringLength(64, ErrorMessage = "Street cannot be longer than 64 characters.")] [Display(Name = "Street")] public string Street { get; set; } [Required] [StringLength(64, ErrorMessage = "Ciy cannot be longer than 64 characters.")] [Display(Name = "City")] public string City { get; set; } [Required] [StringLength(64, ErrorMessage = "Province Name cannot be longer than 64 characters.")] [Display(Name = "Province")] public string Province { get; set; } [Required] [StringLength(10, ErrorMessage = "Post Code cannot be longer than 10 characters.")] [Display(Name = "Post Code")] [DataType(DataType.PostalCode)] public string PostCode { get; set; } [Required] [Display(Name = "Telephone Number")] [StringLength(16, ErrorMessage = "Telephone cannot be longer than 16 characters.")] [DataType(DataType.PhoneNumber)] public string Telephone { get; set; } [Display(Name = "Fax Number")] public string Fax { get; set; } [Required(ErrorMessage = "Email Address is required.")] [Display(Name = "Email")] [DataType(DataType.EmailAddress)] public string Email { get; set; } [Display(Name = "Registration")] public string Registration { get; set; } [Required] [Display(Name = "Creation Date")] [DataType(DataType.Date)] public DateTime CreationDate { get; set; } }I used data annotation for validating data for our schema that is consist on System.ComponentModel.DataAnnotations.Schema . The [Key] annotation work as a primary key in the schema. Then I created a contract schema.
public class Contract { [Key] public int Id { get; set; } [Required] public virtual int CustomerId { get; set; } [ForeignKey("CustomerId")] public virtual Customer Customer { get; set; } [Required] [Display(Name = "Start Date")] [DataType(DataType.Date)] public DateTime StartDate { get; set; } [Required] [Display(Name = "End Date")] [GenericCompare(CompareToPropertyName = "StartDate", OperatorName = GenericCompareOperator.GreaterThanOrEqual)] [DataType(DataType.Date)] public DateTime EndDate { get; set; } [Required] [Display(Name = "License")] public Guid License { get; set; } }The two table has One to zero -one relationship between them. So I created a customer instances in contract class with CustomerId as Foreign key. After creating model we will create a context class. The context class coordinates Entity Framework functionality for a given data model is the database context class. I added model class in the context class.
public class ByWayContext: DbContext { static ByWayContext() { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ByWayContext>()); } public DbSet<Customer> Customers { get; set; } public DbSet<Contract> Contracts { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); Database.SetInitializer<ByWayContext>(null); } }After creating model we will create controller(MVC5 Controller with View, Using entity Framework) based on our model. Now automatically create,edit, list and delete option created for customer model class.
