views:

399

answers:

3

Hello,

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 System.Data.Linq.Mapping.MetaAccessor2.GetBoxedValue(Object instance) +44 System.Data.Linq.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember) +90 System.Data.Linq.StandardTrackedObject.get_HasDeferredLoaders() +102 System.Data.Linq.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary2 visited, Boolean recurse, Int32 level) +187 System.Data.Linq.StandardChangeTracker.Track(Object obj, Boolean recurse) +80 System.Data.Linq.StandardChangeTracker.Track(Object obj) +9 System.Data.Linq.Table1.InsertOnSubmit(TEntity entity) +172 DomainModel.Concrete.SqlRolesRepository.SaveRole(Role role) in C:\inetpub\sklepomat\DomainModel\Concrete\SqlRolesRepository.cs:38 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 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +24 System.Web.Mvc.<>c_DisplayClassa.b_7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +254 System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192 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 _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

+1  A: 

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.

Randy Minder
It was changed to nullable because RoleID is generated in database so I thought that I cannot pass any value and that's the reason of error. I've changed it back to regular int type but it doesn't fix the problem... (see UPDATE)
Kotu
A: 

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);}
        }
}
Kotu
A: 

Great Stuff Kotu. been struggling for 6 hrs. Nice!

Hugh