在模型生成期间检测到一个或多个验证错误 [英] One or more validation errors were detected during model generation

查看:319
本文介绍了在模型生成期间检测到一个或多个验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)实际上我在具有SetExceptionManager的HandlingExceptionPolicy中收到此错误。

2)从选择linq查询语句的位置返回false值。



第二个问题是工作到昨天,突然发生了什么事我不知道。该值不是false,但是语句没有执行。



基本上我使用Visual Studio 2015企业版和SQL Server 2012以及使用Entity Framework连接到数据库6.1.3我的项目是MVC 5 DbContext。



以前使用Visual Studio 2010和SQL Server 2008完成了项目,并使用Entity Framework连接到数据库4.1使用MVC 4 ObjectContext。



我已经更改了一些必须使用SQL Server 2012配置Visual Studio 2015企业版执行的代码。



要将ObjectContext更改为DbContext,我使用了Entity Framework 6 Power Tools。



我尝试过的:



这是第二个问题陈述

1) Actually I am getting this error at HandlingExceptionPolicy having SetExceptionManager.
2) The value is returning false at from where select linq query statement.

The problem second is working till yesterday, suddenly what happened I doesn't know. The value is not false but the statement is not executing.

Basically I am using Visual Studio 2015 Enterprise edition with SQL Server 2012 and the connection to the database using Entity Framework 6.1.3 an my project was MVC 5 DbContext.

Previously the project has been done with Visual Studio 2010 with SQL Server 2008 and the connection to the database using Entity Framework 4.1 with MVC 4 ObjectContext.

I have changed some code that has to be executed with the configuration of Visual Studio 2015 Enterprise edition with SQL Server 2012.

To change the ObjectContext to DbContext I have used Entity Framework 6 Power Tools.

What I have tried:

This is Second problem statement

var objUser = (from u in dbContext.Users
               where u.LoginName.Equals(BO.LoginName)
               select u).FirstOrDefault();





这是第一个问题陈述



This is First problem statement

IConfigurationSource objIConfigurationSource = ConfigurationSourceFactory.Create();

if (objIConfigurationSource.GetSection(LoggingSettings.SectionName) != null)
    Logger.SetLogWriter(new LogWriterFactory(objIConfigurationSource).Create(), throwIfSet: false);
                
ExceptionPolicy.SetExceptionManager(new ExceptionPolicyFactory(objIConfigurationSource).CreateManager(), throwIfSet: false);

if (ExceptionPolicy.HandleException(ex, "Policy_Statement"))
    throw;





我在throw语句中收到错误在模型生成期间检测到一个或多个验证错误



请任何人都可以解释发生了什么这里。我已经向poco类声明了Key属性。





I am getting error at throw statement "One or more validation errors were detected during model generation"

Please anybody can explain what is going on here. I have declared Key attribute to poco classes.

[Key]
[Column(Order = 0)]
public Guid UsersID { get; set; }

[Key]
[Column(Order = 1)]
[StringLength(15)]
public string LoginName { get; set; }

[StringLength(1500)]
public string Password { get; set; }

public Guid? RoleID { get; set; }

[Key]
[Column(Order = 2)]
[StringLength(3)]
public string UserType { get; set; }

public bool? SuperUser { get; set; }

[Key]
[Column(Order = 3)]
public bool IsActive { get; set; }

[Key]
[Column(Order = 4)]
public Guid CreatedBy { get; set; }

[Key]
[Column(Order = 5)]
public DateTime CreatedDate { get; set; }

推荐答案

POCO类有点受损:你已经将整个行声明为复合键!



现在,假设该类表示Users表中的一行,除了UsersId之外,除了键和列顺序装饰之外的所有内容:



That POCO class is a bit mangled: You've declared almost the entire row as a composite key!

Now, assuming that class represents a row in the Users table, strip the key and column order decorations off everything but the UsersId:

public class Users
{
   [Key]
   public Guid UsersID { get; set; }
 
   [StringLength(15)]
   public string LoginName { get; set; }
 
   [StringLength(1500)]
   public string Password { get; set; }
 
   public Guid? RoleID { get; set; }
 
   [StringLength(3)]
   public string UserType { get; set; }
 
   public bool? SuperUser { get; set; }
 
   public bool IsActive { get; set; }
 
   public Guid CreatedBy { get; set; }
 
   public DateTime CreatedDate { get; set; }
}





下一位我有一些最佳实践类型的项目,所以你可以忽略这些项目如果你真的喜欢那里。我强烈建议至少看一看有关如何构建数据及其约束的一些想法。



我们可以做一些其他的修复。首先,你确实不应该得到无法控制的东西;即使您使用默认的guest虚拟机角色,也始终为用户定义角色。处理安全代码时,null值可能会导致意外结果。此外,从逻辑上讲,用户是超级用户似乎很清楚,但这最好由角色封装。看起来你想在那里定义几个关系,所以让我们这样做:





The next bit I have some "best practices" type of items, so you can ignore the items in there if you really like. I would highly suggest taking a look at least to get some ideas about how to structure your data and the constraints on it, though.

We can make some other fixes. First, you have nullables where you really shouldn't; always define a role for users, even if you use a default guest role. A null value can lead to unexpected results when processing security code. Also, it seems logically clear that a user is a SuperUser, but this is best encapsulated by a role. It also looks like you want to define a couple of relationships in there, so let's do that:

public class Users //v2.0
{
   [Key]
   public Guid UsersID { get; set; }
 
   //Changing this because the current code require exactly 15 characters
   [MinLength(3), MaxLength(15)] 
   public string LoginName { get; set; }
 
   // Changing this so that we can change hash algo later without issue
   [MaxLength(1500)]
   public string Password { get; set; }
 
   public Guid RoleID { get; set; }
 
   //This looks like it would be better defined as a relationship, or better
   //still this functionality should be rolled into roles.
   //[StringLength(3)] 
   //public string UserType { get; set; }
 
   //Divvy out Superuser responsibility to roles
   //public bool SuperUser { get; set; }
 
   public bool IsActive { get; set; }
 
   public Guid CreatedById { get; set; }
 
   public DateTime CreatedDate { get; set; }

   [ForeignKey("RoleID")]
   public virtual Role Role { get; set; }

   [ForeignKey("CreatedById")]
   public virtual Users CreatedBy { get; set; }
}


这篇关于在模型生成期间检测到一个或多个验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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