1

I'm trying to add a Metadata partial class to an existing MVC using EF 4.3 and I don't know where to add the class. I'm going throgh the DBFirstMVC tutorial which has Blogs, Posts and Comments. I want to add some validation to the Blog class and after researching on how to do it I see I should add a new Partial Class Blog decorating it with MetadataType(typeof(BlogMetaData))]

It is supposed to go in the same namespace as Blog but when I try to add a class to the Model folder I get a popup saying it already exists do I want to overwrite it. If I say no, I can't create it. Where physically does the Metadata class go?

CD Smith
  • 6,597
  • 7
  • 40
  • 66
Alan Fisher
  • 2,005
  • 4
  • 41
  • 61
  • So basically you are mixing the logical and physical structure in your mind. It can be in a different folder but same namespace. – Ken D Feb 26 '13 at 12:55

2 Answers2

2

You can create a metadata folder an drop the files in it just be sure you keep the same namespace. The location doesnt much matter. You could actually name the file BlogMetadata and include your partial declaration and the metadataclass in it - some choose this approach too.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • I'm really missing something here. So I create the generated classes from the edmx designer and I get the .tt file containing my generated classes. I then need a separate metadata class so I can add my validatin. I did that following code examples but then I get an error when I try to run the application. I only added one of the properties of the generated class to the metadata class and added a required attribute to it. Am I supposed to use every property from the generated class to the metadata class? – Alan Fisher May 30 '12 at 05:05
  • The model backing the 'DBFirstMVCContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data. – Alan Fisher May 30 '12 at 15:16
  • Sorry for the previous comment, I hit an enter thinking it would put me on a new line. I refreshed the edmx file, when I try to run the app, I get the error above. Here is the code from the metadataclass: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace DBFirstMVC.Models { [MetadataType(typeof(BlogMetaData))] public partial class Blog { public class BlogMetaData { [Required] public string Title { get; set; } } } } – Alan Fisher May 30 '12 at 15:25
  • http://aspboss.blogspot.com/2012/03/model-backing-mydbcontext-context-has.html (last option) or http://stackoverflow.com/questions/6157641/ef-code-first-thinks-database-is-out-of-date-only-in-production EF stores information about your model in the database. If you dont care, you can tell it to recreate your database. If you do care you can tell it to not check the edmmetadata table (as in the last line) – Adam Tuliper May 30 '12 at 15:48
0

OK, Got it figured out. Being that I am still learning MVC, I must have deleted the constructor from the DbContext class some time ago and I guess that made the project a Code First project when I was treating it like a Database first. Once I went back and watched Julie Lermans video, I saw the constructor in her video and added it to mine and it works just fine now. Here is exactly what I deleted:

public DBFirstMVCContext()
        : base("name=BlogDataEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
Alan Fisher
  • 2,005
  • 4
  • 41
  • 61