none
How do I persist a plain old C# object into SQL Server using DLinq? RRS feed

  • Question

  • I have a plain old C# class object and I want to persist it to a SQL Server database table.
    The object uses simple types like int and string.

    I have the following DLinq code, I think I am close, what am I missing?

    The error I get is "The type 'PersistObject.BidData' is not mapped as a Table."  How do I map my plain old C# class into DLinq?

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Globalization;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.Linq.Mapping;
    using System.Data.Linq;
    using System.Data.Common;
    
    namespace PersistObject
    {
        class Program
        {
            static void Main(string[] args)
            {
                BidData bidData = new BidData();
                DataContext db = new DataContext(@"server=tcp:mattn2,1433;database=BidTrace;user id=bidTrace;password=bidTrace;");
                db.ExecuteCommand(bidData.createTSQL);
                Table<BidData> bidDataTable = db.GetTable<BidData>();
                bidDataTable.InsertOnSubmit(bidData);
                bidDataTable.Context.SubmitChanges();
            }
        }
        public class BidData
        {
            public string createTSQL
            {
                get
                {
                    return "if exists(select 1 from sys.tables where name like 'bidData') begin drop table bidData end " +
                        "create table BidData(traceId int, eventType varchar(255), eventTag varchar(255)," +
                        "eventPid int, eventTid int, eventClockTime bigint, eventData varchar(max)," +
                        "connectionId int, commandId int, readerId int, connectionOpen bit, connectionClose int," +
                        "readerOpen bit, readerClose bit, commandText varchar(max), connectionString varchar(255))";
                }
            }
            public int traceId;
            [Column(DbType = "varchar(255)")]
            public string eventType;
            [Column(DbType = "varchar(255)")]
            public string eventTag;
            [Column(DbType = "int")]
            public UInt32 eventPid;
            [Column(DbType = "int")]
            public UInt32 eventTid;
            [Column(DbType = "bigint")]
            public long eventClockTime;
            [Column(DbType = "varchar(max)")]
            public string eventData;
            public int connectionId;
            public int commandId;
            public int readerId;
            public bool connectionOpen;
            public bool connectionClose;
            public bool readerOpen;
            public bool readerClose;
            [Column(DbType = "varchar(max)")]
            public string commandText;
            [Column(DbType = "varchar(255)")]
            public string connectionString;
        }
    }
    

    • Moved by simmdanMicrosoft employee Tuesday, July 28, 2009 4:35 AM wrong forum for this question (From:ADO.NET Entity Framework and LINQ to Entities)
    Monday, July 27, 2009 9:08 PM

Answers