Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;


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; }
Posted
Updated 23-Oct-17 2:03am

1 solution

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:

C#
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; }
}


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:

C#
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; }
}
 
Share this answer
 
Comments
Member 8583441 24-Oct-17 0:22am    
hello Nathan Minier sir, I am having small doubt regarding MinLength, MaxLength attribute and StringLength attribute
Member 8583441 24-Oct-17 0:29am    
I studied differences between them with this link https://stackoverflow.com/questions/5717033/stringlength-vs-maxlength-attributes-asp-net-mvc-with-entity-framework-ef-code-f

from this link i can understand that StringLength can be used in this case whereas MaxLength is used for array size. Is it right that i am thinking sir
Nathan Minier 25-Oct-17 7:20am    
I did some more reading (I should know better than to trust anything other than source docs by now) and the formulation you're using will indeed only affect maxlength, but that leaves us with the minlength problem; you don't want "" or string.Empty to be valid values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900