1

I'm following some tutorial about ASP.net MVC on visual Studio, and currently I'm doing the part about DataBase, Code First

I do not understand why, but my DataAnnotations do not seem to be taken into account.

[Table("Restos")]
public class Resto
{
    public int Id { get; set; }
    [Required][MaxLength(10)]
    public string Nom { get; set; }
    public string Telephone { get; set; }
}

In fact, I created a class "Resto", so the name of the table was automatically named "Restoes", but I don't want this name, I want "Restos", without "e" So to oblige my "Restoes" table to call itself "Restos", I used the [Table ("Restos")] annotation but it didnt change its name, And the [Required] annotation on one of the Fields in my "Restoes" table also has no effect because the "Nullable" property is always at True.

Could you help me please ?

Smit Patel
  • 2,992
  • 1
  • 26
  • 44
Matthieu Veron
  • 290
  • 6
  • 16
  • what exactly you need to do with it?, do you need the table name as it as in database? – Smit Patel May 26 '17 at 13:23
  • Currently my table is named "Restoes", but I want "Restos", with the DataAnnotation [Table ("Restos")] But it's on the tutoriel, so it's not a "real need", it is just to understand why it doesnt works – Matthieu Veron May 26 '17 at 13:24
  • 2
    Have you run the update script after you've modified your database? Or have you tried to uninstall your database (since this is for tutorial and I assume doesn't contain anything important) and re-create it using the new class with attributes? – artgdev May 26 '17 at 13:39
  • I created a class "Resto", so the name of the table was automatically named "Restoes", but I don't want this name, I want "Restos"- this is a manual process which can't be achieved by the entityframework code first. – Smit Patel May 26 '17 at 13:40
  • What is the update script ? I tried to disconnect and connect again to the database – Matthieu Veron May 26 '17 at 13:41
  • I'm not sure to understand. I can give you the link of the tutorial if you want to see what it's written (but it's in french) https://openclassrooms.com/courses/apprendre-asp-net-mvc/le-modele-36 The issue is that the plural of resto is not the good one i want, so the solution proposed by the tutorial is to use DataAnnotation. But anyway, when I'm trying to use other DataAnnotation, like [Required] or [Maxlenght], it still doesnt works I may be missing something for the changes to be made – Matthieu Veron May 26 '17 at 13:43
  • Did you scaffold the migration in Package Manager Console after you have added the Required or MaxLength attribute? – papadoble151 May 26 '17 at 13:49
  • How am I suppose to do that ? – Matthieu Veron May 26 '17 at 13:51
  • You navigate to Tools -> Nuget Package Manager -> Package Manager Console, after that you have to scaffold a migration, that will update the database. [Link](https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx) – papadoble151 May 26 '17 at 14:08
  • I'm sorry but I dont know what is "Scaffold a migration" – Matthieu Veron May 26 '17 at 14:29
  • You need code first migrations or you can use a [database initializer](http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx) such as DropCreateDatabaseIfModelChanges. – Steve Greene May 26 '17 at 16:19

1 Answers1

1

It seems to me that you didn't update the database. When you change your models (add annotations, rename/create fields and so on), you have to apply those changes to the database as well. It is called a migration. It can be performed from Package Manager console (Tools -> Nuget Package Manager -> Package Manager Console) . First and foremost. All the commands from the console should be targeted on the project, containing the Context class (any class that inherits DbContext, this is BddContext class in your case, according to the tutorial).

  1. Enable migrations in your project: open the PM console, choose the required project from the dropdown, type the following command "Enable-Migrations" and press Enter
  2. Once the migrations are succesfuly enabled, create one, by executing command "Add-Migration"
  3. Once the migration is created, execute the "Update-Database" command

Check thoroughly the migration file that is being created. In fact this file will contain a list of changes that will be applied to the database. You can read more about migrations here. Strongly advise you doing that if you want to work with Entity Framework

papadoble151
  • 646
  • 8
  • 19
  • I got an error during the Udpate-Migration : Error Number:1832,State:1,Class:14 Cannot attach the file 'C:\Users\MVeron\documents\visual studio 2013\Projects\ChoixResto\ChoixResto\App_Data\BddContextt.mdf' as database 'BddContextt'. – Matthieu Veron May 30 '17 at 09:16
  • @MatthieuVeron see this https://stackoverflow.com/questions/17012839/cannot-attach-the-file-mdf-as-database – papadoble151 May 30 '17 at 09:39
  • Ok thanks. I tried to follow the indication on the link you send me. But it doesnt work : http://hpics.li/a000fa5 – Matthieu Veron May 30 '17 at 10:15
  • @MatthieuVeron Then try this workaround https://stackoverflow.com/a/20176660/6294253 By the way, it seems like you are on step 3. Is there any relevant content in the migration file? – papadoble151 May 30 '17 at 10:19
  • I did some modifications in my ConnexionString, but it doesnt work, so i cancelled this modifications, and now I can't see my table in my db anymore :( when I click on "tables", there is nothing .. I think it's because I remove the db and reconnect its again. I'm gonna check your link above, thanks – Matthieu Veron May 30 '17 at 10:30
  • Same error with the following connectionString : – Matthieu Veron May 30 '17 at 10:36
  • @MatthieuVeron create a completely new empty database. Then - conduct a migration upon it. Add the connection like this https://msdn.microsoft.com/en-us/library/jj193542(v=vs.113).aspx. Delete your old one – papadoble151 May 30 '17 at 10:39
  • To delete the old one, I just have to click on its on the server explorer and press touch delete ? Or have I to do an other thing ? I dont really know how to create a new empty db .. Sorry but I'm new on VS, and I just wanted to follow an easy tutorial but it was complicated... – Matthieu Veron May 30 '17 at 10:42
  • @MatthieuVeron yes and delete it from Web.config file as well – papadoble151 May 30 '17 at 10:44
  • Ok, so I created a new class "Newdb.cs" on model And after I tried to connect to its, but a pop-up appeared : "This db doesnt exist, would you like to create it". So I chose Yes, but now I dont have my tables on this db. So now have I to do the migration, or did I do something wrong ? – Matthieu Veron May 30 '17 at 10:56
  • @MatthieuVeron, exactly, you have to conduct a migration now – papadoble151 May 30 '17 at 11:16
  • Ok fine. When I do the command "Enable-Migrations", it ask me "More than one context type was found in the assembly 'ChoixResto'. To enable migrations for 'BddContext', use Enable-Migrations -ContextTypeName BddContext. To enable migrations for 'Newdb', use Enable-Migrations -ContextTypeName Newdb." So I chose option 2 – Matthieu Veron May 30 '17 at 11:42
  • After I do the command Add-Migration, but I had this error : "PM> Add-Migration cmdlet Add-Migration at command pipeline position 1 Supply values for the following parameters: Name: New Migration Unable to generate an explicit migration because the following explicit migrations are pending: [201705300913302_mig1]. Apply the pending explicit migrations before attempting to generate a new explicit migration." – Matthieu Veron May 30 '17 at 11:42
  • @MatthieuVeron, learn to read and understand the erros and messages. Since you have a pending migration to a database that does not exist - you are free to delete it. After the old migration has been deleted - scaffold and apply the new one – papadoble151 May 30 '17 at 11:44
  • To be honnest, like I said i'm a beginner and I get confused by myself because I have 2 projects : the principal project "ChoixRestos" and the test project "ChoixRestos.Test to make automatic tests. I tried to make the migration and it's sound to be the solution for my problem, but nothing work like I would like. Now, the name of my table is "Restos", like I wanted, but when I'm trying to rename on an other name to see if I can do the same thing again, it's doesnt work, and by the way the other DataAnnotation didnt work (MaxLenght for example). I should delete my project and do it again maybe – Matthieu Veron May 30 '17 at 12:13
  • @MatthieuVeron if it throws an exception (with message "context has changed since the database was created") after you change your model, it is absolutely ok. You have changed your model in your code, but not your database. All that needs to be done - is another migration, it's just how it works. You create/edit your model, you make a migration and update your database to fit your model – papadoble151 May 30 '17 at 12:17
  • I think it works :o A big thank for taking all this time to help me. A last question : In the tutorial I follow, the author don't speak about migration. So maybe there is a solution for all this to be automatic ? – Matthieu Veron May 30 '17 at 12:25
  • @MatthieuVeron can't say it is a good idea, but here is a link for automatics migrations https://msdn.microsoft.com/en-us/library/jj554735(v=vs.113).aspx personalIy I prefer to apply changes to the database manually – papadoble151 May 30 '17 at 12:36
  • I think I'm going to kill myself !! http://hpics.li/dd443ff Because the error "cannot attach the file ... as database ..." when i do the update database is here again. So i tried to delete the database and create it again, but I definitely cant do that ... – Matthieu Veron May 30 '17 at 14:15