Editing The Data Model

Feb 22, 2010 at 5:43 PM

Hi,

I am trying to add a column to the Comments table.  However I am having trouble working with the DBML file.  I tried to regenerate the file before making data model changes to make sure I could still build afterwords and that failed.

I also tried adding the column to the DBML file through visual studio but that also results in the project being unbuildable.

Can you provide guidance on now to generate the DBML file?

Coordinator
Feb 23, 2010 at 6:11 AM

I just do it the old fashioned way... deleting tables in Visual studio, refresh your DB, then drag them back into the dbml

Feb 24, 2010 at 4:27 PM
Edited Feb 24, 2010 at 5:17 PM

That resolved my build problem, and I was able to add my column and propagate it up the UI. 

However now I've stumbled into another.   Now when I try to change group/subgruop assignments the '.HASLoadedOrAssignedValue' property is set to true causing it to always throw ForeignKeyReferenceAlreadyHasValueException when updating the ticket.  (This is my first time working with LINQ to SQL)

 

  public static ticket Update(stDataContext db, int ticket_id, int status, int priority_id, int assigned_to)
        {
            ticket t = Get(db, ticket_id);
            t.ticket_status = status;
            t.priority = priority_id;
            t.assigned_to_group_last = t.assigned_to_group;
            t.assigned_to_group = assigned_to; //db.user_groups.Single(f => f.id == assigned_to).id;
            if (status == 5) t.closed = DateTime.Now;
            else t.closed = NullDate;
            t.last_action = DateTime.Now;
            db.SubmitChanges();
            return t;
        }

 

 

I did not change any code related to the Ticket or Groups.

 

  public static ticket Update(stDataContext db, int ticket_id, int status, int priority_id, int assigned_to)

      {

            ticket t = Get(db, ticket_id);

            t.ticket_status = status;

            t.priority = priority_id;

            t.assigned_to_group_last = t.assigned_to_group;

            t.assigned_to_group = assigned_to; //  t.assigned_to_groupdb.sub_units.Single(f => f.id == assigned_to).id;

            if (status == 5) t.closed = DateTime.Now;

            else t.closed = NullDate;

            t.last_action = DateTime.Now;

            db.SubmitChanges();

            return t;

        }

The bolded is the last line in non-generate code getting called before entering the LINQ-SQL file.  I did some research, and it stated that you should update the key using an entity and not the value so i tried the code commented out as a possible fix, but that did not resolve the issue.

 

 

Any suggestions as to where/what I should be looking at to try and fix this issue?  I'm a complete noob when it comes to LINQ-to-SQL.

Thanks,

Tim

Feb 25, 2010 at 12:11 PM

Okay I figured out and fixed my problem.  I'm a little confused as to why it's not an issue with Slickticket in general.

 

When using LINQ and you need to update a FK your not supposed to do it by value. (i.e t.assigned_to_group = assigned_to)

It's suppsoed to be done updating the Entity reference:  t.sub_unit = db.sub_units.Single(f => f.id == assigned_to);

Coordinator
Mar 1, 2010 at 4:03 PM

Linq-to-SQL does not require you update the reference - you can simply update the value; it is only in Linq-to-Entities that this is necessary... you can see it throughout the release code and it works just fine.  Something in your environment must be different/missing.