×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

问一个LINQ to SQL的问题,执行DataContext.SubmitChanges() 数据库并不更新,该如何解决?代码如内:

本文发表在 rolia.net 枫下论坛using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;

namespace linqTest
{
class Program
{
static void Main(string[] args)
{
DataContext dc = new DataContext(@"Data Source=xxx;Initial Catalog=xxx;Integrated Security=True");
dc.Log = Console.Out;
Table<Customer> customerArray = dc.GetTable<Customer>();

// 执行到这里确实有T-SQL select 语句输出
Customer cc = customerArray.Single(s => s.NameAbbreviation == "Toronto");

cc.Name = "Beijing: "+DateTime.Now.ToLocalTime();

// 按理说执行到这里应该有相应的Update语句输出,可是就是什么都没有
dc.SubmitChanges();


Console.ReadLine();
}
}

[Table(Name = "dbo.Customer")]
class Customer
{

[Column]
public int CustomerID;

[Column]
public string Name;

[Column]
public string NameAbbreviation;

public override string ToString()
{
return CustomerID.ToString() + "," + Name + "," + NameAbbreviation;
}
}
}

编译运行没有任何问题,因为有
dc.Log = Console.Out;
所以所有生成的T-SQL语句都会动态的显示在Console上,当执行
Customer cc = customerArray.Single(s => s.NameAbbreviation == "Toronto");
的时候,LINQ显示出它所生成的T-SQL select 语句
但是当执行
dc.SubmitChanges();
的时候,没有任何T-SQL输出,然后就结束了,当然数据库也没有任何更新

这代码我是按照教科书上写的,那教科书可能是按照VS2008的Beta版写的,是不是正式发行的LINQ有了一些改变?

跪清高人出手。。。。更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 问一个LINQ to SQL的问题,执行DataContext.SubmitChanges() 数据库并不更新,该如何解决?代码如内:
    本文发表在 rolia.net 枫下论坛using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Data;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Linq;
    using System.Linq.Expressions;
    using System.ComponentModel;
    using System;

    namespace linqTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    DataContext dc = new DataContext(@"Data Source=xxx;Initial Catalog=xxx;Integrated Security=True");
    dc.Log = Console.Out;
    Table<Customer> customerArray = dc.GetTable<Customer>();

    // 执行到这里确实有T-SQL select 语句输出
    Customer cc = customerArray.Single(s => s.NameAbbreviation == "Toronto");

    cc.Name = "Beijing: "+DateTime.Now.ToLocalTime();

    // 按理说执行到这里应该有相应的Update语句输出,可是就是什么都没有
    dc.SubmitChanges();


    Console.ReadLine();
    }
    }

    [Table(Name = "dbo.Customer")]
    class Customer
    {

    [Column]
    public int CustomerID;

    [Column]
    public string Name;

    [Column]
    public string NameAbbreviation;

    public override string ToString()
    {
    return CustomerID.ToString() + "," + Name + "," + NameAbbreviation;
    }
    }
    }

    编译运行没有任何问题,因为有
    dc.Log = Console.Out;
    所以所有生成的T-SQL语句都会动态的显示在Console上,当执行
    Customer cc = customerArray.Single(s => s.NameAbbreviation == "Toronto");
    的时候,LINQ显示出它所生成的T-SQL select 语句
    但是当执行
    dc.SubmitChanges();
    的时候,没有任何T-SQL输出,然后就结束了,当然数据库也没有任何更新

    这代码我是按照教科书上写的,那教科书可能是按照VS2008的Beta版写的,是不是正式发行的LINQ有了一些改变?

    跪清高人出手。。。。更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 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
    • 深蓝说的没错,千万别手动生成LINQ的datacontext,费力不讨好,我不是高人,简单说说我目前在项目里怎么做的
      首先在project里new一个新Item,选择LINQ to SQL Classes,然后双击进入Design View界面,在Tools->Connect to Database,把需要的Tables选上,拖拽到DesignView,vs2008就会自动生成DataClassesDataContext的所有需要的代码,最后给你几行最基本的Update的sample,在page的cs code里:
      DataClassesDataContext db = new DataClassesDataContext();
      Guid gID = new Guid(Request.QueryString["ID"].ToString());
      var rec = db.Receivings.Single(r => r.ID == gID);
      rec.ReceivingDate = Convert.ToDateTime(ReceivingDateTxt.Text);
      db.SubmitChanges();

      简单就是美,如果LINQ搞那么复杂就失去了其中一个很大的魅力了:)
      • 多谢二位兄台回帖,问题原因已经找到,是因为我没有把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;
        }
        }
        • 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.
          • 嗯,俺现在是想尽可能了解Linq to SQL的细节,所以才会在Data Access层中自己写这些基本的Mapping。因为我的Bussiness Object层通过一些固定的Interface来与Data Access通讯,以后用Designer做O/R Mapping只要改写这些Interface就好了,工作量不会很大
            • 哥们潜力无限