如何使用 Linq to SQL 在数据库中添加记录?

发布于 2024-10-31 17:03:45 字数 287 浏览 0 评论 0原文

我刚刚学习 Linq to SQL,通过一个示例,我通过传递自定义类型对象在 db 中添加了一条记录。例如,要在用户表中添加新用户:

db.Users.InsertOnSubmit(newUser);
db.SubmitChanges();

我无法理解 Linq 如何知道应在哪一列中添加哪个属性值?

有没有比这更好的方法使用 Linq 在数据库中添加新记录?

还请提及如果我不使用自定义类型(DTO),我该如何添加?

感谢您宝贵的时间和分享。

I am just learning Linq to SQL and from an example I added a record in db by passing a custom type object. For example to add a new user in users table I did:

db.Users.InsertOnSubmit(newUser);
db.SubmitChanges();

I couldn't get how Linq know which property value should be added in which column ?

Is there any better way then this to add a new record in db using Linq ?

Kindly also mention how I can add if I am not using custom types (DTO) ?

so thanks for your precious time and sharing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£烟消云散 2024-11-07 17:03:45

DBML 文件详细说明了所有对象的属性如何映射到 DBMS。添加到 DBML 的数据库表/视图会导致为您创建相应的类(例如您的 Users 类),其属性用告诉 L2S 如何处理

DBML 文件下的 所有 ORM 映射的属性进行修饰应该是显示这一点的 [whatever].designer.cs 文件。这是它应该是什么样子的示例:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Invoices")]
public partial class Invoice : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private long _InvoiceId;

    private string _InvoiceNum;

    private decimal _TotalTaxDue;

snip

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_InvoiceId", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public long InvoiceId
    {
        get
        {
            return this._InvoiceId;
        }
        set
        {
            if ((this._InvoiceId != value))
            {
                this.OnInvoiceIdChanging(value);
                this.SendPropertyChanging();
                this._InvoiceId = value;
                this.SendPropertyChanged("InvoiceId");
                this.OnInvoiceIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_InvoiceNum", DbType="VarChar(15)")]
    public string InvoiceNum
    {
        get
        {
            return this._InvoiceNum;
        }
        set
        {
            if ((this._InvoiceNum != value))
            {
                this.OnInvoiceNumChanging(value);
                this.SendPropertyChanging();
                this._InvoiceNum = value;
                this.SendPropertyChanged("InvoiceNum");
                this.OnInvoiceNumChanged();
            }
        }
    }

Your DBML file spells out how all your objects' properties are mapped to your DBMS. the database tables/views you add to the DBML causes corresponding classes to be created for you—like your Users class—whose properties are decorated with attributes telling L2S how to handle all the ORM mappings

Under your DBML file should be a [whatever].designer.cs file that shows this. Here's a sample of what it should look like:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Invoices")]
public partial class Invoice : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private long _InvoiceId;

    private string _InvoiceNum;

    private decimal _TotalTaxDue;

snip

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_InvoiceId", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public long InvoiceId
    {
        get
        {
            return this._InvoiceId;
        }
        set
        {
            if ((this._InvoiceId != value))
            {
                this.OnInvoiceIdChanging(value);
                this.SendPropertyChanging();
                this._InvoiceId = value;
                this.SendPropertyChanged("InvoiceId");
                this.OnInvoiceIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_InvoiceNum", DbType="VarChar(15)")]
    public string InvoiceNum
    {
        get
        {
            return this._InvoiceNum;
        }
        set
        {
            if ((this._InvoiceNum != value))
            {
                this.OnInvoiceNumChanging(value);
                this.SendPropertyChanging();
                this._InvoiceNum = value;
                this.SendPropertyChanged("InvoiceNum");
                this.OnInvoiceNumChanged();
            }
        }
    }

etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文