J'utilise approche du Code d'abord pour créer ma base de données de mon modèle et j'utilisé les données annotations dans ma classe comme ceci:Code de Entity Framework First Data Annotation MaxLength et le tableau ne fonctionne pas
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("Cat1")]
public class Cat
{
public int ID { get; set; }
[Required]
[MaxLength(50)]
public string Nombre { get; set; }
[StringLength(500)]
public string Descripción { get; set; }
}
using System.Data.Entity;
public class Contexto: DbContext {
public DbSet<Cat> Cats { get; set; }
protected override void OnModelCreating(DbModelBuilder MB) {
MB.Conventions.Remove<PluralizingTableNameConvention>();
MB.Conventions.Remove<OneToManyCascadeDeleteConvention>();
MB.Conventions.Remove<ForeignKeyIndexConvention>();
MB.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
MB.ComplexType<Departamento>();
MB.ComplexType<Porción>();
}
}
Mais pour certains raison que seules les annotations [Obligatoire] et [StringLength] sont appliquées. Ma base de données se termine comme ceci:
TABLE [dbo].[Cats] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Nombre] NVARCHAR (MAX) NOT NULL,
[Descripción] NVARCHAR (500) NULL,
CONSTRAINT [PK_dbo.Cats] PRIMARY KEY CLUSTERED ([ID] ASC)
);
Je me demande pourquoi certaines annotations travaillent et d'autres ne le font pas. Je sais que je peux utiliser 'StringLength' au lieu de 'MaxLength', mais je n'ai pas d'autre option pour 'Table' et d'autres qui pourraient avoir le même problème. Il a l'habitude de bien fonctionner pendant un certain temps, mais a soudainement cessé de fonctionner. Comment puis-je faire à nouveau fonctionner les annotations 'MaxLenght' et 'Table'? Edit: les autres annotations ne fonctionnent pas: ComplexType et NotMapped.
J'utilise EF 6.1.3 et targetframework net40 et Visual Studio 2017.
Merci!
Afficher la méthode 'OnModelCreating' –
Je l'ai édité pour afficher la méthode 'OnModelCreating', je ne l'ai pas ajouté en premier car il n'avait rien à voir avec la classe 'Cat' mais peut-être qu'il me manque quelque chose là ... – David
Soudain? Réviser vos commits. –