Results 1 to 3 of 3

Thread: [RESOLVED] MVC5 Code-First Database Schema

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] MVC5 Code-First Database Schema

    Two things, I am new to MVC and need to confirm if this looks right. Also I am told by the IDE that a public string is not nullable. The database would have a nullable varchar type.
    vb Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel.DataAnnotations;
    4. using System.ComponentModel.DataAnnotations.Schema;
    5. using System.Linq;
    6. using System.Web;
    7.  
    8. namespace NotaryNet.Models
    9. {
    10.     /// <summary>
    11.     /// Registration Database Structure.
    12.     /// </summary>
    13.     /// <remarks>
    14.     /// 1. Company name during registration must be unique. Not allow same company name to be registered.
    15.     /// 2. Initial registered user is automatically assigned to the Admin Role and they can register/create additional users as needed.
    16.     ///     A. If any additional user accounts are created by the Admin, they will be added to the Users role.
    17.     /// 3. Each SystemUser has their own respective information about them in the SystemUsersInfo table.
    18.     ///
    19.     /// 1. Company and SystemUsers share a PK (CompanyID) and FK (CompanyID) relationship.
    20.     /// 2. SystemUsers and SystemUsersInfo share a PK (UserID) and FK (UserID) relationship.
    21.     /// </remarks>
    22.  
    23.     [Table("Companies")]
    24.     public class Register
    25.     {
    26.         [Key]
    27.         public int CompanyID { get; set; }
    28.         public string CompanyName { get; set; }
    29.         public bool IsActive { get; set; }
    30.         public virtual ICollection<SystemUsers> SystemUsers { get; set; }
    31.     }
    32.  
    33.     [Table("SystemUsers")]
    34.     public class SystemUsers
    35.     {
    36.         [Key]
    37.         public int UserID { get; set; }
    38.         public string Username { get; set; }
    39.         public string Password { get; set; }
    40.         public bool IsActive { get; set; }
    41.         [ForeignKey("CompanyID")]
    42.         public virtual ICollection<SystemUserInfo> SystemUserInfo { get; set; }        
    43.     }
    44.  
    45.     [Table("SystemUserInfo")]
    46.     public class SystemUserInfo
    47.     {
    48.         [Key]
    49.         public int SystemUserInfoID { get; set; }
    50.         public string NamePrefix { get; set; }
    51.         public string FirstName { get; set; }
    52.         public string? MiddleInitial { get; set; }
    53.         public string LastName { get; set; }
    54.         public string? NameSuffix { get; set; }
    55.         [ForeignKey("UseID")]
    56.     }
    57. }

  2. #2

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: [RESOLVED] MVC5 Code-First Database Schema

    I give up!
    Well that didn't take long I know what you mean, though, as I found it frustrating at first. It does get easier so it's worth sticking with it.

    Also I am told by the IDE that a public string is not nullable.
    You don't need to set a string to be nullable in order to generate a nullable varchar. A string will generate a nullable varchar by default and you need to decorate the property with a Required attribute if you want the field to be non-nullable.

    Ints, boleans and numerics behave the opposite way and generate a non-nullable type by default. If you want them to be nullable you have to declare them as nullable types.

    That inconsistency is one of the easiest things to get wrong at first.

    Also, beware that foreign keys are all set to cascade deletes and updates by default (which is absoluetly the wrong choice for a default IMO) and you have to explicitely set them to refuse delates and updates.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width