none
String PK is null when is shouldn't be RRS feed

  • Question

  • I have a simple class with 2 properties:

    public string ParentName { getset; }
    public string Name { getset; }

    the mapping is :

        <Type Name="Angel.Core.Domain.CorporationUnit">
          <Column Name="CorporationUnitType" Member="CorporationUnitType" DbType="Int NOT NULL" IsDiscriminator="true" />
          <Column Name="Name" Member="Name" DbType="NVarChar(100) NOT NULL" IsPrimaryKey="true" />
          <Column Name="ParentName" Member="ParentName" DbType="NVarChar(100)" CanBeNull="true" />

     

    when I write the following code:

                Department dep = new Department("LEVEL1");
                dep.Depth = 0;
                dep.ParentName = "";

    and try to insert it I get :

    Cannot insert the value NULL into column 'Name', table 'Angel.dbo.CorporationUnits'; column does not allow nulls. INSERT fails.
    The statement has been terminated


     

    why is that? myble LTS try to create a rows and after that to set it data?

     

    Monday, July 18, 2011 10:49 AM

All replies

  • I don't see anywhere in the code you provided that will set a value for the Name column.  Perhaps the constructor for Department does not set the Name column (I am assuming that the "LEVEL1" parameter is supposed to be that value.

     

    LS


    Lloyd Sheen
    Monday, July 18, 2011 2:47 PM
  • the constructor sets the value currectly. when I hover over the Name property right before the insert, it does have a value LEVEL1 so I don't know why I get exception.


    does LTS has problem with PK that is not int?


    Monday, July 18, 2011 3:04 PM
  • There should be no problem with an alpha PK.  Can you show us the code that is used to insert the row?
    Lloyd Sheen
    Monday, July 18, 2011 4:48 PM
  • the code is :

     

     

            [Test]
            public void CreateDepartment()
            {
                serviceFactory = new ClassServiceFactory();
                ICorporationUnitManager cm = (ICorporationUnitManager)serviceFactory.FindByServiceName(CORMAN);

                Department dep = new Department("LEVEL1");
                dep.Name = "test";
                dep.Depth = 0;
                dep.ParentName = "";

                cm.CreateCorporationUnit(dep); <= this calls method below
    }

            public CorporationUnit CreateCorporationUnit(CorporationUnit newC)
            {
                IServiceFactory serviceFactory = new ClassServiceFactory();
                IDaoFactory df = (IDaoFactory)serviceFactory.FindByServiceName(SERVICENAME);
                ICorporationUnitDao icud = df.GetCorporationUnitDao();

               icud.Save(newC); <= below
                return newC;
            }

            }

            public virtual T Save(T entity)
            {
                ITable tab = db.GetTable(entity.GetType().BaseType);
                tab.InsertOnSubmit(entity);
                this.CommitChanges(); <= method below
                return entity;

            }
            public virtual void CommitChanges()
            {
                try
                {
                    db.SubmitChanges(ConflictMode.ContinueOnConflict); <=
    /* this cause the Cannot insert the value NULL into column 'Name', table 'Angel.dbo.CorporationUnits'; column does not allow nulls. INSERT fails.
    The statement has been terminated. */
                }
                catch (ChangeConflictException e)
                {
                    //Log Message to somewhere
                    //e.Message;

                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepChanges);
                    }
                    db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                }
            }


    the mapping is:

      <Table Name="CorporationUnits" Member="CorporationUnits" >
        <Type Name="Angel.Core.Domain.CorporationUnit">
          <Column Name="CorporationUnitType" Member="CorporationUnitType" DbType="Int NOT NULL" IsDiscriminator="true" />
          <Column Name="Name" Member="Name" DbType="NVarChar(100) NOT NULL IDENTITY" IsPrimaryKey="true" />
          <Column Name="ParentName" Member="ParentName" DbType="NVarChar(100)" CanBeNull="true" />
          <!--Association Name="CorporationUnits_Parents" Member="Parent" ThisKey="Name" OtherKey="ParentName"  DeleteRule="NO ACTION" IsUnique="false" /-->
          <!-->Association Name="CorporationUnits_Parents" Member="SubUnits" ThisKey="ParentName" OtherKey="Name"  IsForeignKey="true" /-->
          <Type Name="Angel.Core.Domain.Department" InheritanceCode="0" IsInheritanceDefault="True">
           
          </Type>

          <Type Name="Angel.Core.Domain.Section" InheritanceCode="1" >
          </Type>
        </Type>
      </Table>

     

     

    Tuesday, July 19, 2011 5:40 AM
  • You've defined your CorporationUnitType field as not allowing nulls.

     

    <Column Name="CorporationUnitType" Member="CorporationUnitType" DbType="Int NOT NULL" IsDiscriminator="true" />

     

    And, you are not setting this field to something or even referencing it in what you are trying to insert.  When this happens, it will contain a null value which you don't allow...

     

                Department dep = new Department("LEVEL1");
                dep.Depth = 0;
                dep.ParentName = "";

     

     

    Tuesday, July 19, 2011 2:45 PM
  • the CorporationUnitType is a discriminator column so LTS fills the data for it. no need to set it manualy and the exceptiom message specificaly says Name field is null
    Cannot insert the value NULL into column 'Name', table 'Angel.dbo.CorporationUnits'; column does not allow nulls. INSERT fails.
    The statement has been terminated
    Tuesday, July 19, 2011 8:52 PM
  • Thank you for posting.

    I suggest you take a look at the actual SQL generated. Start up SQL Server Profiler and start a trace. Please check for more information about SQL Server Profiler. http://msdn.microsoft.com/en-us/library/ms187929.aspx

    In addition, if you have any finding, please feel free to let us know.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 20, 2011 11:10 AM
  • unfortunately I have SQL Express and SQL profiler is not included with this version. I don't understand it at all. Just a table with two columns and the insert fails..

    maybe someone can give me the complete code & mapping for this? just a table with two column of strings which one is PK and with a discriminator key for hierarchy of department and section? I've tried doing this but getting to my corent mapping which fails..
    Friday, July 22, 2011 4:32 PM
  • Hello again,

    Regarding ADO.NET of Linq to SQL DataContext, here's a simple sample was you can check. http://msdn.microsoft.com/en-us/library/Bb386944(v=VS.90).aspx

    Please feel free to let us know how it goes.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, July 24, 2011 3:26 PM
  • IsDbGenerated is for identity columns.  I am now looking at code I had to use the profiler in SQL Express.  This would give the info that we are missing.

     

     


    Lloyd Sheen
    Sunday, July 24, 2011 4:04 PM
  • I have the code and it works for SQL Express.  I would like to know how I can get this to anyone that would like to have a profiler when using SQL EXpress.

     

    It is a windows form app which I may decide to WPF.

    Let me know how I can get the app to you


    Lloyd Sheen
    Sunday, July 24, 2011 4:15 PM
  • I have the code and it works for SQL Express.  I would like to know how I can get this to anyone that would like to have a profiler when using SQL EXpress.

     

    It is a windows form app which I may decide to WPF.

    Let me know how I can get the app to you


    Lloyd Sheen

    which code is that? are you saying that it is working for you?
    Sunday, July 24, 2011 4:36 PM
  • It is my own code for using the profiler API with SQL Express.

    I have a winforms app that I wrote that will execute against SQL Express.  It needs a folder to be created to store the profiler info and correct permissions to allow the file to be deleted for each profiler session.

     


    Lloyd Sheen
    Sunday, July 24, 2011 5:45 PM
  • I've found the problem. Maybe someone can explain it to me.

    The class hierarchy is as follow:

     public abstract class BaseEntity<T>
        {
            public BaseEntity()
            { }
            private T id = default(T);
            /// </summary>
            public virtual T ID
            {
                get { return id; }
                set { id = value; }
    }


    public class CorporationUnit : BaseEntity<string>
        {
            public string Name
            {
                get
                {

                    return base.ID;
                }
                set
                {
                    base.ID = value;
                }
            }
            public string ParentName { getset; }
            public int Depth { getset; }
            public int CorporationUnitType { getset; }
    }

     public class Department : CorporationUnit 
        {
          
            public new string Name
            {
                get
                {
                    return base.Name;
                }
                set
                {
                    base.Name = value;
                }
            }

     

    when I comment out the Name property of class Department somehow it is working ok but I don't understand why. the property return the base property and if it is doesn't exist the base property also the one is in use...




    Monday, July 25, 2011 12:24 PM
  • It looks that behind code looks good. just my sight, it may be caused by you have invoked submitchanges method twice. did you try to remove one which was located in exception block?

    others, i found some questions like yours, maybe help you. check them as follows.

    http://stackoverflow.com/questions/4444407/entity-framework-ctp-4-cannot-insert-the-value-null-into-column-even-though

    http://www.codeproject.com/KB/linq/SettingDefaultValues.aspx

    http://stackoverflow.com/questions/1331338/linq-to-sql-insert-failing

    Thanks,

    Werewolf,


    Just a newbie for everything.
    Tuesday, July 26, 2011 9:28 AM
  • It wasn't because  a second submitinsert but due to a property in the deparment claa with a modifier new as

    public new string Name ...

    which just return and set the base Name property so I don't know why it didn't work. Just before submit the base property does have a value and not null so something is broken with LTS maybe with a new modifer... the bae class is generic so maybe it cause the problem

     

    Tuesday, July 26, 2011 8:14 PM