0

I created an mvc 4 web project using database first and entity framework but was surprised to see that default models were not generated automatically, is there a way to create them from my .edmx file or are there any other possibilities to save hand coding them?

Any help would be much appreciated

UPDATE:

Sorry, should have mentioned I am looking for a solution to generate simple models with basic methods such as :

public class EditUserModel
{
    public int ID { get; set; }

    [Required(ErrorMessage = "Forename is required")]
    public string Forename { get; set; }

    [Required(ErrorMessage = "Surname is required")]
    public string Surname { get; set; }

    [Required(ErrorMessage = "Username is required")]
    public string Username { get; set; }
}
Mike
  • 369
  • 1
  • 6
  • 24
  • 1
    there is a problem with vs 2012 and EF5 that doesn't always generate properly. You also need to make sure you build after adding the edmx file. If you expand the edmx file you should see something like `Model.context.tt` and `Model.tt` right click both and select run custom tool. Be sure to build again after that. – Eonasdan Oct 30 '12 at 11:58
  • thanks for your reply but the code it generates is hard to read, I was looking for a simpler solution as per my update above, thanks Mike – Mike Oct 30 '12 at 12:21
  • I think the only way you're going to get something like that is to do code first. If you really just want the `[required(...` part you can create a partial class that adds the meta data. I can provide an example if you want/need – Eonasdan Oct 30 '12 at 12:24
  • I really want to avoid code first if possible as I want to retain control of my field names, I'm not worried about generating the [Required attributes, just the data properties really to save time, do you think this is possible? – Mike Oct 30 '12 at 12:26
  • guess I'm not sure what you're looking for. You can already do `TABLENAME.COLUMN` in your code. If you want values, query your entities as `db.TABLENAME.Where(x => x.forname == "foo")` if you want to save values `var foo.forname = "bar";` – Eonasdan Oct 30 '12 at 13:09

1 Answers1

0

I ended up making a SQL query to generate a simple mvc model from my tables.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Mike
  • 369
  • 1
  • 6
  • 24