Quantcast
Channel: MySQL Forums - Connector/NET and C#, Mono, .Net
Viewing all articles
Browse latest Browse all 1451

Entity Framework and MySql with concurrency check (no replies)

$
0
0
I'm having trouble getting concurrency checking working in Entity Framework with MySql. I created the following code which demonstrates the issue. It works up until the point where it tries to select the updated timestamp field but adds to the where clause a condition of the timestamp field being equal to the previous value. Obviously if the timestamp field was the same as previously there would be no point in retrieving it from the database. Am I doing something wrong?

DROP DATABASE IF EXISTS `BugReport`;
CREATE DATABASE IF NOT EXISTS `bugreport`;
USE `BugReport`;

DROP TABLE IF EXISTS `test`;
CREATE TABLE IF NOT EXISTS `test` (
`TestId` int(10) NOT NULL AUTO_INCREMENT,
`AStringField` varchar(50) DEFAULT NULL,
`DateModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`TestId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

-- Dumping data for table BugReport.test: ~1 rows (approximately)
/*!40000 ALTER TABLE `test` DISABLE KEYS */;
INSERT INTO `test` (`TestId`, `AStringField`, `DateModified`) VALUES
(1, 'Initial Value', '2014-07-09 15:20:21');


-- Dumping structure for trigger BugReport.BeforeUpdate
DROP TRIGGER IF EXISTS `BeforeUpdate`;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='';
DELIMITER //
CREATE TRIGGER `BeforeUpdate` BEFORE UPDATE ON `test` FOR EACH ROW BEGIN
SET NEW.DateModified = NOW();
END//
DELIMITER ;

using System;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace BugReport
{
class Program
{
static void Main(string[] args)
{
using (var context = new BugReportModel())
{
context.Database.Log = (s => Console.WriteLine(s));

var firstTest = context.tests.First();
firstTest.AStringField = "First Value";

try
{
// Exception is thrown when changes are saved.
context.SaveChanges();
}
catch (DbUpdateConcurrencyException e)
{
Console.WriteLine(e.ToString());
}

Console.ReadLine();
}
}
}

public class BugReportModel : DbContext
{
public BugReportModel()
: base("name=Model1")
{

}

public virtual DbSet<test> tests { get; set; }
}


[Table("test")]
public class test
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TestId { get; set; }

[StringLength(50)]
public string AStringField { get; set; }

[ConcurrencyCheck()]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[Column(TypeName = "timestamp")]
public System.DateTime DateModified { get; set; }
}
}

Viewing all articles
Browse latest Browse all 1451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>