This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / 问一个LINQ to SQL的问题,执行DataContext.SubmitChanges() 数据库并不更新,该如何解决?代码如内:
-binghongcha76(一只大猫);
2008-8-22
{1817}
(#4634723@0)
-
You don’t properly create entity class Customer. In order to for DataContext object to work, the correct mapping is important.The better way is to use O/R Designer to create entity classes (LINQ to SQL classes), rather than create them manually. Read Walkthrough: Creating LINQ to SQL Classes (O/R Designer) from following url
-deep_blue(BLUE);
2008-8-24
{289}
(#4637559@0)
-
深蓝说的没错,千万别手动生成LINQ的datacontext,费力不讨好,我不是高人,简单说说我目前在项目里怎么做的
-mfcguy();
2008-8-24
{598}
(#4637605@0)
-
多谢二位兄台回帖,问题原因已经找到,是因为我没有把Customer Table的CustomerID设置为 PrimaryKey,这样LINQ在更新的时候就不知道该以什么为基准,从而忽略掉我的Update请求。修改后的代码如内只要把Customer Table改成如下样式即可,这一招也是CSDN C#论坛上的一位高人帮我搞定的
[Table(Name = "dbo.Customer")]
class Customer
{
// 必须设置IsPrimaryKey,这一步在LINQ Update,Insert和Delete的时候非常重要
[Column(DbType = "Int Not Null", IsPrimaryKey = true)]
public int CustomerID;
[Column]
public string Name;
[Column]
public string NameAbbreviation;
public override string ToString()
{
return CustomerID.ToString() + "," + Name + "," + NameAbbreviation;
}
}
-binghongcha76(一只大猫);
2008-8-25
{493}
(#4638672@0)
-
Of cource, in your backend database table there must be at least one PK field. Otherwise, even O/R Designer cannot generate correct entity class for you.I still strongly suggust you to use O/R Designer rather than code it by hand, unless you are trying to understand its mechanism.
It’s easy to miss many features in manually coded entity classes.
For example, the CustomerID field still misses one important attribute, IsDbGenerated=true,
Although in your current scenario, it’s OK. Howerver, if the field is IDENTITY and when you try to insert new record, you will get error for sure.
-deep_blue(BLUE);
2008-8-25
{459}
(#4639117@0)
-
嗯,俺现在是想尽可能了解Linq to SQL的细节,所以才会在Data Access层中自己写这些基本的Mapping。因为我的Bussiness Object层通过一些固定的Interface来与Data Access通讯,以后用Designer做O/R Mapping只要改写这些Interface就好了,工作量不会很大
-binghongcha76(一只大猫);
2008-8-25
(#4639227@0)
-
哥们潜力无限
-mfcguy();
2008-8-25
(#4640009@0)