Page 1 of 1

Assign EntitySet and preserve autogenerated ID's

Posted: Mon 14 Oct 2013 08:19
by lc4pro
Hi,

I'm currently struggeling with a EntitySet

Code: Select all

CREATE TABLE `a` (
    `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `name` varchar(256) NULL DEFAULT NULL
);
CREATE TABLE `b` (
    `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `a_id` INT(11) NULL DEFAULT NULL,
    `name` varchar(256),
    CONSTRAINT `FK` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`)
)
To respect the short living context approach in my asp.net cms I read the entity set to a localy hold list, manipulate it and on save rewrites it to a new context to the database. This works all well but the EntitySet generates new ID's everytime they are saved to database.

Code: Select all

Protected Property bList As EntitySet(Of b)
    Get
        Return Session("bList")
    End Get
    Set(value As EntitySet(Of b))
        Session("bList") = value
    End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim db = New DbContext()
    var Current = db.a.SingleOrDefault(Function(x) x.Id = CurrentRequest.Id)
    bList = Current.b
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click, btnSaveAndNew.Click
    Dim db = New DbContext()
    Current = db.a.SingleOrDefault(Function(x) x.Id = CurrentRequest.Id)
    Current.b = bList
    db.SubmitChanges()
End Sub

Code: Select all

UPDATE b SET a_id = :p1 WHERE id = :key1 
-- p1: Input Int (Size = 0; DbType = Int32) [] 
-- key1: Input Int (Size = 0; DbType = Int32) [33495] 
-- Context: Devart.Data.MySql.Linq.Provider.MySqlDataProvider Mapping: AttributeMappingSource Build: 4.2.327.0 
 
INSERT INTO b (a_id) VALUES (:p1) 
-- p1: Input Int (Size = 0; DbType = Int32) [12749] 
-- Context: Devart.Data.MySql.Linq.Provider.MySqlDataProvider Mapping: AttributeMappingSource Build: 4.2.327.0 
This would leave me with data corpses and a bloated database. Is there a easy way to preserve ID's on a disconected EntitySet on reintegration?

Thanks for help!

Re: Assign EntitySet and preserve autogenerated ID's

Posted: Mon 14 Oct 2013 13:22
by MariiaI
Please send us a small test project, so that we are able to investigate your scenario in more details and find the more suitable solution for you.