locked
How to create supplier model for CRUD like identity RRS feed

  • Question

  • User-570626059 posted

    Hi guys,

    so similar to how identity works with user model. you can add-migration to update the db and then can access user properties like user.username etc etc.

    How can I do the same but for a supplier model? is this even possible?

    suppliername

    supplierid

    suppliertelephone

    thanks

    Wednesday, July 4, 2018 10:58 AM

Answers

  • User283571144 posted

    Hi skyblue28,

    From you description , I think you used code first from database instead of  empty code first. If you use this mode,  your sqlserver database has already had tables.

    However, when you use migration ,it will create table which has already existed again ,so we should synchronize your model with your database firstly.

    More details, you could refer to below steps:

    First ,generate original model from sql table, please don’t make change to it.

    public partial class Supplier
        {
            public int SupplierID { get; set; }
     
            [Required]
            [StringLength(40)]
            public string CompanyName { get; set; }
     
            [StringLength(30)]
            public string ContactName { get; set; }
     
            [StringLength(30)]
            public string ContactTitle { get; set; }
     
            [StringLength(60)]
            public string Address { get; set; }
     
            [StringLength(15)]
            public string City { get; set; }
     
            [StringLength(15)]
            public string Region { get; set; }
     
            [StringLength(10)]
            public string PostalCode { get; set; }
     
            [StringLength(15)]
            public string Country { get; set; }
     
            [StringLength(24)]
            public string Phone { get; set; }
     
            [StringLength(24)]
            public string Fax { get; set; }
     
            [Column(TypeName = "ntext")]
            public string HomePage { get; set; }
     
         
     
    }
    

    Type  enable-migrations  and then  add-migration –name  <migration name>  to package manager console. The following class will be created.

    public partial class test : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.Suppliers",
                    c => new
                        {
                            SupplierID = c.Int(nullable: false, identity: true),
                            CompanyName = c.String(nullable: false, maxLength: 40),
                            ContactName = c.String(maxLength: 30),
                            ContactTitle = c.String(maxLength: 30),
                            Address = c.String(maxLength: 60),
                            City = c.String(maxLength: 15),
                            Region = c.String(maxLength: 15),
                            PostalCode = c.String(maxLength: 10),
                            Country = c.String(maxLength: 15),
                            Phone = c.String(maxLength: 24),
                            Fax = c.String(maxLength: 24),
                            HomePage = c.String(storeType: "ntext"),
                        })
                    .PrimaryKey(t => t.SupplierID);
                
            }
            
            public override void Down()
            {
                DropTable("dbo.Suppliers");
            }
        }
    

    Please commont out the createTable statement ,because the supplier has existed in the database and type update-database or migration will send DDL sql statement to the database.

    public override void Up()
            {
                //CreateTable(
                //    "dbo.Suppliers",
                //    c => new
                //        {
                //            SupplierID = c.Int(nullable: false, identity: true),
                //            CompanyName = c.String(nullable: false, maxLength: 40),
                //            ContactName = c.String(maxLength: 30),
                //            ContactTitle = c.String(maxLength: 30),
                //            Address = c.String(maxLength: 60),
                //            City = c.String(maxLength: 15),
                //            Region = c.String(maxLength: 15),
                //            PostalCode = c.String(maxLength: 10),
                //            Country = c.String(maxLength: 15),
                //            Phone = c.String(maxLength: 24),
                //            Fax = c.String(maxLength: 24),
                //            HomePage = c.String(storeType: "ntext"),
                //        })
                //    .PrimaryKey(t => t.SupplierID);
                
            }
     
    

    Then  update your model. The following is my modified model.

    public partial class Supplier
        {
            public int SupplierID { get; set; }
     
            [Required]
            [StringLength(40)]
            public string CompanyName { get; set; }
     
            [StringLength(30)]
            public string ContactName { get; set; }
     
            [StringLength(30)]
            public string ContactTitle { get; set; }
     
            [StringLength(60)]
            public string Address { get; set; }
     
            [StringLength(15)]
            public string City { get; set; }
     
            [StringLength(15)]
            public string Region { get; set; }
     
            [StringLength(10)]
            public string PostalCode { get; set; }
     
            [StringLength(15)]
            public string Country { get; set; }
     
            [StringLength(24)]
            public string Phone { get; set; }
     
            [StringLength(24)]
            public string Fax { get; set; }
     
            [Column(TypeName = "ntext")]
            public string HomePage { get; set; }
     
            [Column(TypeName = "datetime2")]
            public DateTime Whencompliant { get; set; }
     
            [Column(TypeName = "datetime2")]
            public DateTime Reviewdate { get; set; }
     
    }
    

    At last type add-migration –name <migration name>.The following class will be created.

    public partial class test1 : DbMigration
        {
            public override void Up()
            {
                AddColumn("dbo.Suppliers", "Whencompliant", c => c.DateTime(nullable: false, precision: 7, storeType: "datetime2"));
                AddColumn("dbo.Suppliers", "Reviewdate", c => c.DateTime(nullable: false, precision: 7, storeType: "datetime2"));
            }
            
            public override void Down()
            {
                DropColumn("dbo.Suppliers", "Reviewdate");
                DropColumn("dbo.Suppliers", "Whencompliant");
            }
    }
    

    Now you could type  update-database, the change will be reflected to the table in your database.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 9, 2018 5:14 AM

All replies

  • User475983607 posted

    How can I do the same but for a supplier model? is this even possible?

    Of course it is possible.  The database design drives this requirement and the schema is driven by code first.  Create a entity model of Supplier which has the fields that define a Supplier.  Then create a UserSupplier Entity that has SupplierId and UserId fields.   The UserSupplier table relates the User to a Supplier.  If the user can be many different types then Change the Supplier table to a UserTypes table and add fields that define a UserType.  Anyway this is called a one-to-many relationship and there is a lot of information on how to build this sort of thing.

    http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

     

    Wednesday, July 4, 2018 11:19 AM
  • User-570626059 posted

    OK I am a bit stuck.

    I created code first entity model using the wizard.

    It created this class

        public partial class SupplierDBModel : DbContext
        {
            public SupplierDBModel()
                : base("name=SupplierDBModel")
            {
    
                
    
            }
    
            public virtual DbSet<supplier> suppliers { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
            }
        }

    and also this class called supplier (same name as the table)

     public partial class supplier
        {
            public int supplierid { get; set; }
    
            [Required]
            [StringLength(50)]
            public string suppliername { get; set; }
    
            [StringLength(50)]
            public string address1 { get; set; }
    
            [StringLength(50)]
            public string county { get; set; }
    
            [StringLength(50)]
            public string country { get; set; }
    
            [StringLength(50)]
            public string telephone { get; set; }
    
            [StringLength(50)]
            public string address2 { get; set; }
    
            [StringLength(11)]
            public string postcode { get; set; }
    
            [StringLength(20)]
            public string webstatus { get; set; }
    
            [StringLength(50)]
            public string status { get; set; }
    
            [StringLength(50)]
            public string LastreviewedBy { get; set; }
    
    
            [Column(TypeName = "datetime2")]
            public DateTime Whencompliant { get; set; }
    
            [Column(TypeName = "datetime2")]
            public DateTime Reviewdate { get; set; }
    
        }

    this is based on the current fields in the sql table. I have added the bottom two datetime fields in the class. I want to now migrate and update the database.

    When I do this no changes are found.

    What else do I need to do?

    Thursday, July 5, 2018 10:00 AM
  • User283571144 posted

    Hi skyblue28,

    From you description , I think you used code first from database instead of  empty code first. If you use this mode,  your sqlserver database has already had tables.

    However, when you use migration ,it will create table which has already existed again ,so we should synchronize your model with your database firstly.

    More details, you could refer to below steps:

    First ,generate original model from sql table, please don’t make change to it.

    public partial class Supplier
        {
            public int SupplierID { get; set; }
     
            [Required]
            [StringLength(40)]
            public string CompanyName { get; set; }
     
            [StringLength(30)]
            public string ContactName { get; set; }
     
            [StringLength(30)]
            public string ContactTitle { get; set; }
     
            [StringLength(60)]
            public string Address { get; set; }
     
            [StringLength(15)]
            public string City { get; set; }
     
            [StringLength(15)]
            public string Region { get; set; }
     
            [StringLength(10)]
            public string PostalCode { get; set; }
     
            [StringLength(15)]
            public string Country { get; set; }
     
            [StringLength(24)]
            public string Phone { get; set; }
     
            [StringLength(24)]
            public string Fax { get; set; }
     
            [Column(TypeName = "ntext")]
            public string HomePage { get; set; }
     
         
     
    }
    

    Type  enable-migrations  and then  add-migration –name  <migration name>  to package manager console. The following class will be created.

    public partial class test : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.Suppliers",
                    c => new
                        {
                            SupplierID = c.Int(nullable: false, identity: true),
                            CompanyName = c.String(nullable: false, maxLength: 40),
                            ContactName = c.String(maxLength: 30),
                            ContactTitle = c.String(maxLength: 30),
                            Address = c.String(maxLength: 60),
                            City = c.String(maxLength: 15),
                            Region = c.String(maxLength: 15),
                            PostalCode = c.String(maxLength: 10),
                            Country = c.String(maxLength: 15),
                            Phone = c.String(maxLength: 24),
                            Fax = c.String(maxLength: 24),
                            HomePage = c.String(storeType: "ntext"),
                        })
                    .PrimaryKey(t => t.SupplierID);
                
            }
            
            public override void Down()
            {
                DropTable("dbo.Suppliers");
            }
        }
    

    Please commont out the createTable statement ,because the supplier has existed in the database and type update-database or migration will send DDL sql statement to the database.

    public override void Up()
            {
                //CreateTable(
                //    "dbo.Suppliers",
                //    c => new
                //        {
                //            SupplierID = c.Int(nullable: false, identity: true),
                //            CompanyName = c.String(nullable: false, maxLength: 40),
                //            ContactName = c.String(maxLength: 30),
                //            ContactTitle = c.String(maxLength: 30),
                //            Address = c.String(maxLength: 60),
                //            City = c.String(maxLength: 15),
                //            Region = c.String(maxLength: 15),
                //            PostalCode = c.String(maxLength: 10),
                //            Country = c.String(maxLength: 15),
                //            Phone = c.String(maxLength: 24),
                //            Fax = c.String(maxLength: 24),
                //            HomePage = c.String(storeType: "ntext"),
                //        })
                //    .PrimaryKey(t => t.SupplierID);
                
            }
     
    

    Then  update your model. The following is my modified model.

    public partial class Supplier
        {
            public int SupplierID { get; set; }
     
            [Required]
            [StringLength(40)]
            public string CompanyName { get; set; }
     
            [StringLength(30)]
            public string ContactName { get; set; }
     
            [StringLength(30)]
            public string ContactTitle { get; set; }
     
            [StringLength(60)]
            public string Address { get; set; }
     
            [StringLength(15)]
            public string City { get; set; }
     
            [StringLength(15)]
            public string Region { get; set; }
     
            [StringLength(10)]
            public string PostalCode { get; set; }
     
            [StringLength(15)]
            public string Country { get; set; }
     
            [StringLength(24)]
            public string Phone { get; set; }
     
            [StringLength(24)]
            public string Fax { get; set; }
     
            [Column(TypeName = "ntext")]
            public string HomePage { get; set; }
     
            [Column(TypeName = "datetime2")]
            public DateTime Whencompliant { get; set; }
     
            [Column(TypeName = "datetime2")]
            public DateTime Reviewdate { get; set; }
     
    }
    

    At last type add-migration –name <migration name>.The following class will be created.

    public partial class test1 : DbMigration
        {
            public override void Up()
            {
                AddColumn("dbo.Suppliers", "Whencompliant", c => c.DateTime(nullable: false, precision: 7, storeType: "datetime2"));
                AddColumn("dbo.Suppliers", "Reviewdate", c => c.DateTime(nullable: false, precision: 7, storeType: "datetime2"));
            }
            
            public override void Down()
            {
                DropColumn("dbo.Suppliers", "Reviewdate");
                DropColumn("dbo.Suppliers", "Whencompliant");
            }
    }
    

    Now you could type  update-database, the change will be reflected to the table in your database.

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 9, 2018 5:14 AM