Pergunta

The data is inserted using LINQ to SQL, the id is generated but the database table is empty.

Using a stored procedure there is no problem. But inserting using linq the id is generated everytime but the table is empty.

The code is below:

Int32 t = 2;

using (EduDataClassesDataContext db =new EduDataClassesDataContext())
{
    using (var scope = new TransactionScope())
    {
        db.Connection.ConnectionString = Common.EdukatingConnectionString;
        UserLogin userlog = new UserLogin();
        userlog.Username = userinfo.Username;
        userlog.Password = userinfo.Password;
        userlog.UserTypeId = t;
        userlog.FullName = userinfo.FullName;
        db.UserLogins.InsertOnSubmit(userlog);
        db.SubmitChanges();
        Int64 n = userlog.Id;
        UserInformation userinfor = new UserInformation();
        userinfor.FirstName = userinfo.FirstName;
        userinfor.LastName = userinfo.LastName;
        userinfor.MobileNum = userinfo.MobileNum;
        userinfor.Email = userinfo.Email;
        userinfor.Gender = userinfo.Gender;
        userinfor.Address = userinfo.Address;

        userinfor.UserLoginId = n;
        userinfor.CreatedBy = n;
        userinfor.OrganizationName = userinfo.OrganizationName;
        userinfor.DateOfBirth = userinfo.DateOfBirth;
        userinfor.CreatedDate = DateTime.Now;
        db.UserInformations.InsertOnSubmit(userinfor);
        db.SubmitChanges();
    }
}
Foi útil?

Solução

When you are using a TransactionScope, you need to call the Complete method in order to Commit the transaction in the DataBase.

using (var db = new EduDataClassesDataContext())
using (var scope = new TransactionScope())
{
    ...
    db.UserInformations.InsertOnSubmit(userinfor);
    db.SubmitChanges();

    // The Complete method commits the transaction. If an exception has been thrown,
    // Complete is not  called and the transaction is rolled back.
    scope.Complete();
}

Failing to call this method aborts the transaction, because the transaction manager interprets this as a system failure, or exceptions thrown within the scope of transaction.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top