简体   繁体   English

C#linq:如何将对象作为新记录提交到数据库

[英]c# linq: how to submit object to database as a new record

I have the following object: 我有以下对象:

namespace LearnLINQ1
{
    [Table(Name="testMe")]
    public class SubmitTest
    {
        [Column(Name="FirstName")]
        public string FirstName { get; set; }

        [Column(Name = "LastName")]
        public string LastName { get; set; }

        [Column(Name = "PhoneNumber")]
        public int PhoneNumber { get; set; }

        linqLayerDataContext db;
    }
}

And i've used the same thing with a constructor: 而且我在构造函数中使用了相同的东西:

namespace LearnLINQ1
{
    [Table(Name="testMe")]
    public class SubmitTest
    {
        [Column(Name="FirstName")]
        public string FirstName { get; set; }

        [Column(Name = "LastName")]
        public string LastName { get; set; }

        [Column(Name = "PhoneNumber")]
        public int PhoneNumber { get; set; }

        linqLayerDataContext db;

        //constructor
        public SubmitTest(string first, string last, int phone, linqLayerDataContext db)
        {
            this.FirstName = first;
            this.LastName = last;
            this.PhoneNumber = phone;
            this.db = db;
        }

    }
}

I'm using the following code to instantiate the object and attempt to add it to the database as a new record: 我正在使用以下代码实例化该对象,并尝试将其作为新记录添加到数据库中:

SubmitTest test = new SubmitTest { FirstName = "Jeremy", LastName = "Stafford", PhoneNumber = 23 };
db.testMes.InsertOnSubmit(test);

But im getting the error: 但即时通讯收到错误:

Error 1 The best overloaded method match for 'System.Data.Linq.Table.InsertOnSubmit(LearnLINQ1.testMe)' has some invalid arguments C:\\Users\\Jeremy\\Documents\\Visual Studio 2010\\Projects\\LearnLINQ1\\LearnLINQ1\\Form1.cs 42 13 LearnLINQ1 错误1'System.Data.Linq.Table.InsertOnSubmit(LearnLINQ1.testMe)'的最佳重载方法匹配具有一些无效的参数C:\\ Users \\ Jeremy \\ Documents \\ Visual Studio 2010 \\ Projects \\ LearnLINQ1 \\ LearnLINQ1 \\ Form1.cs 42 13学习LINQ1

Error 2 Argument 1: cannot convert from 'LearnLINQ1.SubmitTest' to 'LearnLINQ1.testMe' C:\\Users\\Jeremy\\Documents\\Visual Studio 2010\\Projects\\LearnLINQ1\\LearnLINQ1\\Form1.cs 42 39 LearnLINQ1 错误2参数1:无法从'LearnLINQ1.SubmitTest'转换为'LearnLINQ1.testMe'C:\\ Users \\ Jeremy \\ Documents \\ Visual Studio 2010 \\ Projects \\ LearnLINQ1 \\ LearnLINQ1 \\ Form1.cs 42 39 LearnLINQ1

I'm not sure if there was something im missing in the class definition... kinda new to the concept. 我不确定类定义中是否缺少某些东西……对这个概念来说有点新。 Can someone point me in the right direction? 有人可以指出我正确的方向吗?

================================ UPDATE: Ok so i made some changes ===============================更新:好的,所以我做了一些更改

the customer class: 客户类别:

namespace LearnLINQ1
{

    public class Customer
    {
        [Table(Name = "testMe")]
        public class SubmitTest
        {
            [Column(Name = "FirstName")]
            public string FirstName { get; set; }

            [Column(Name = "LastName")]
            public string LastName { get; set; }

            [Column(Name = "PhoneNumber")]
            public int PhoneNumber { get; set; }
        }
    }
}

And the test code: 和测试代码:

namespace LearnLINQ1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            linqTestDataContext db = new linqTestDataContext();

            Table<Customer> Customers = db.GetTable<Customer>();
            var cus = new Customer { FirstName = "Jeremy", Lastname = "Stafford", Age = 31 };
            db.Customers.InsertOnSubmit(cus);

            db.SubmitChanges();
        }
    }
}

Now the problem is with the customer class. 现在问题出在客户类别上。 it is giving the following error: 它给出以下错误:

Error 1 Missing partial modifier on declaration of type 'LearnLINQ1.Customer'; 错误1在声明类型'LearnLINQ1.Customer'时缺少部分修饰符; another partial declaration of this type exists C:\\Users\\Jeremy\\Documents\\Visual Studio 2010\\Projects\\LearnLINQ1\\LearnLINQ1\\Customer.cs 11 18 LearnLINQ1 存在此类型的另一个部分声明C:\\ Users \\ Jeremy \\ Documents \\ Visual Studio 2010 \\ Projects \\ LearnLINQ1 \\ LearnLINQ1 \\ Customer.cs 11 18 LearnLINQ1

LearnLINQ1.testMe是您需要创建的对象,而不是LearnLINQ1.SubmitTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM