Reputation: 95
I've a table Role associated in 1-many relation with table User in my database. I've created LINQ mapping classes manually:
[Table(Name="Role")]
public class Role
{
private EntitySet<User> _Users;
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int RoleID { get; set; }
[Column] public string Name { get; set; }
[Association(Name = "FK_User_Role", Storage = "_Users", ThisKey = "RoleID", OtherKey = "RoleID")]
public EntitySet<User> Users
{
get{ return this._Users; }
set{ this._Users.Assign(value);}
}
}
The problem is that EntitySet Users can't be nullable so if later than I wish to create new role:
public override void CreateRole(string roleName)
{
try
{
Role new_role = new Role();
new_role.Name = roleName;
_RolesRepository.SaveRole(new_role);
}
catch
{
throw;
}
}
I'm getting error message listed below:
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 36: else Line 37: { Line 38:
rolesTable.InsertOnSubmit(role); ins = true; Line 39: } Line 40:Source File: C:\inetpub\sklepomat\DomainModel\Concrete\SqlRolesRepository.cs
Line: 38
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Data.Linq.Mapping.EntitySetDefSourceAccessor2.GetValue(T instance) +15
2.GetBoxedValue(Object instance) +44 System.Data.Linq.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember) +90
System.Data.Linq.Mapping.MetaAccessor
System.Data.Linq.StandardTrackedObject.get_HasDeferredLoaders() +102
System.Data.Linq.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary2 visited, Boolean recurse, Int32 level) +187
1.InsertOnSubmit(TEntity entity) +172 DomainModel.Concrete.SqlRolesRepository.SaveRole(Role role) in C:\inetpub\sklepomat\DomainModel\Concrete\SqlRolesRepository.cs:38
System.Data.Linq.StandardChangeTracker.Track(Object obj, Boolean recurse) +80 System.Data.Linq.StandardChangeTracker.Track(Object obj) +9 System.Data.Linq.Table
DomainModel.Concrete.SklepomatRoleProvider.CreateRole(String roleName) in C:\inetpub\sklepomat\DomainModel\Concrete\SklepomatRoleProvider.cs:71 System.Web.Security.Roles.CreateRole(String roleName) +73
WebUI.Controllers.TempController.ble() in C:\inetpub\sklepomat\WebUI\Controllers\TempController.cs:29
lambda_method(ExecutionScope , ControllerBase , Object[] ) +74
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +178
2 parameters) +24
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc.<>c__DisplayClassa.b__7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +254
1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
System.Web.Mvc.Controller.ExecuteCore() +126
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
I assume that's because EntitySet<User> _Users
equals to null
...
Am I right !?
What should be assigned to _Users
if it can't by nullable !?
UPDATE:
Exception is raised
try { rolesTable.InsertOnSubmit(role);}
catch (Exception ex)
{
throw ex;
}
Where role object's fields are set to:
_Users = null,
Name = "New name",
RoleID = 0,
Users = null
Upvotes: 0
Views: 4046
Reputation: 95
Here's a link to great tutorial related to LINQ mapping: http://www.codeproject.com/KB/linq/linqtutorial.aspx My final code is:
[Table(Name="Role")]
public class Role
{
private EntitySet<User> _Users = new EntitySet<User>();
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int RoleID { get; set; }
[Column] public string Name { get; set; }
[Association(Name = "FK_User_Role", Storage = "_Users", ThisKey = "RoleID", OtherKey = "RoleID")]
public EntitySet<User> Users
{
get{ return this._Users; }
set{ this._Users.Assign(value);}
}
}
Upvotes: 4
Reputation: 48452
You've got a problem in your code and/or schema. The Role.RoleID property, which is your primary key, is a nullable int type. This cannot be. Primary key values cannot contain null values.
Upvotes: 1