MVC 5 ModelValidation错误 [英] MVC 5 ModelValidation error

查看:122
本文介绍了MVC 5 ModelValidation错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My Clinic Class:

public class Clinic
    {
        [Key]
        public int ClinicId { get; set; }
        [Display(Name="Clinic Name")]
        public string ClinicName { get; set; }
        [Display(Name = "Clinic Location")]
        public string ClinicLocation { get; set; }
    }

My DataContext:

public class DataContext: DbContext
    {
        public DataContext()
            : base("Padcc")
        {

        }

        public DbSet<Clinic> Clinics { get; set; }

public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
        {
            return base.Set<TEntity>();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        }
    }

I have the method in my repository:

public void InsertClinic(Clinic c)
        {
              clinic.Clinics.Add(c);
        }
However when I redirect to the Create Clinic View and fill in the view, it give me an error:

System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:



DataAccess.Clinic: : EntityType 'Clinic' has no key defined. Define the key for this EntityType.





请帮助



Please help

推荐答案

这篇关于MVC 5 ModelValidation错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆