开发者

How to check if object is sql to linq type

开发者 https://www.devze.com 2022-12-25 01:05 出处:网络
anyone know if there\'s any easy way to check if an object is an sql to linq type? I have a method like this but would like it to only accept valid types.

anyone know if there's any easy way to check if an object is an sql to linq type? I have a method like this but would like it to only accept valid types.

public static void Update(params object[] items)
{
    using (TheDataContext dc = new TheDataContext())
    {
        System.Data.Linq.ITable table;

        if(items.Length > 0)
            table = dc.GetTable(items[0].GetType());

        for (int i = 0; i < items.Length; i++开发者_JS百科)
        {
            table.Attach(items[i]);
            dc.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, 
                items[i]);
        }

        dc.SubmitChanges();
    }
}


You will want to access the MetaModel instance returned from the Mapping property on the DataContext instance.

With that, you can call the GetTable method, passing the Type of the model that you want to determine is supported in your current LINQ-to-SQL scenario.

Note, this is just in the model, if you want to check for it's existence in the database, then that's a different story, you will have to handle that yourself. However, that's not too difficult. From the MetaTable instance returned from GetTable, you can use the TableName property to get the name of the table as it exists in the database, and then query the database for the existence of the table, as per the mapping on the model.

0

精彩评论

暂无评论...
验证码 换一张
取 消