locked
Adding a table RRS feed

  • Question

  • User323983933 posted

    Visual Studio 2015 MVC 

    AS is the way of things, I added a table to the SQL Server database.

    I see no easy way to add the Model (like I did when I created the database originally, as Add->Ado.Net Entity Model feels it absolutely MUST not overwrite the ORM (object relational model) object.

    So other than manually creating a blank project, importing all the data tables and views over again, and then doing the WinMerge session from Hell on 50+ tables, there SHOULD be a way to ADD a new model for a table, and UPDATE the ORM automatically.

    Or am I missing something?

    Saturday, July 6, 2019 4:32 AM

All replies

  • User753101303 posted

    Hi,

    And you used which option. then? If your "source of truth" is the database you can refresh the EDMX file or generated code from the database. Is your issue that you customized generated files? When doing that you should really use the "partial class" feature so that you can keep your customization in your own files allowing to recreate generated files without loosing anything.

    Saturday, July 6, 2019 8:33 AM
  • User1120430333 posted

    Ef6 using DB first, then you can update the ORM model from the database schema automatically using the wizar I think you can do it with Code first too automatically. wit its 'existing database option' using the wizard.

    Saturday, July 6, 2019 2:19 PM
  • User-474980206 posted
    Your mistake was modifying the generated code. You should never do this, instead use partial classes and extension methods.
    Saturday, July 6, 2019 7:21 PM
  • User323983933 posted

    I could use a tutorial on this.  So I have

    public partial class Customer
    1. Where do I put my new models?
    2. I assume all my added properties just go into my own classes.
    3. So I have some new classes - I scaffold those just like I had done for the existing classes.
    4. I can then scaffold my own classes just like I previously did for the generated ones.
    5. The edits I typically make to the existing models now are things like [Display(name=)] and formatting datetimes to plain dates, etc.  Is there a way to do THAT (EF property decorations) in a derived class?
    6. This would also mean that I have to blow away and reimport my models and for every change to the database.
    7. What is the standard way to name the new classes. something like make Customer into myCustomer?

    I'm thinking of making a folder in Models called "my" and adding all the derived classes there.  That way I can blow away the partials easily and reimport them.

    Saturday, July 6, 2019 7:42 PM
  • User409696431 posted

    An example for one of my sites:

    Models
       Fossil.edmx
        [Auto generated files from DB-first design, including:]
         Fossil.tt
           Age,cs
           Class.cs
           [etc,  (A ,cs file for each table)]
       FossilMetaData.cs  <== My file containing all the partial classes containing my meta data for the tables  This file is not overridden when you make changes to the database. Thus your meta data for the existing tables are saved when you update edmx from the database.  Manually edit this file to add meta data for new tables you add.

    Sample:

       Age.cs

    namespace Fossils.Models
    {
        using System;
        using System.Collections.Generic;
        
        public partial class Age
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public Age()
            {
                this.Fossils = new HashSet<Fossil>();
            }
        
            public int AgeId { get; set; }
            public string Age1 { get; set; }
            public Nullable<int> ImageId { get; set; }
        
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<Fossil> Fossils { get; set; }
        }
    }

    And in FossilMetaData.cs there is a section for Age:

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    namespace Fossils.Models
    {
    
    ,,,,    
    
    [MetadataType(typeof(AgeMetaData))]
        public partial class Age
        {
            public class AgeMetaData
            {
                [Display(Name = "Age")]
                public int AgeId { get; set; }
    
    
                [Display(Name = "Age")]
                [StringLength(100, ErrorMessage = "Must be less than 100 characters")]
                public string Age1 { get; set; }
            }
        }
    
    ...
    }

    Saturday, July 6, 2019 10:16 PM