datacontext

What does LINQ-to-SQL Table<T>.Attach do?

时光毁灭记忆、已成空白 提交于 2019-12-01 02:26:36
What exactly does the LINQ-to-SQL method Table<T>.Attach() and Table<T>.AttachAll() and what is an example/situation for their proper usage? Also, please check out this related question: How to detach a LINQ-to-SQL data object from the DataContext's tracking mechanism? It is really useful in multi-tier applications that serialize/deserialize data to other layers. Short version: Attach() tells DataContext the entity is not new (for insert ) but an updated entity that is meant to be updated in the DB. Long version: You have a DataContext where your entities exist. New entities get inserted,

Linq to sql add/update in different methods with different datacontexts

只谈情不闲聊 提交于 2019-12-01 01:16:20
I have to methods, Add() and Update() which both create a datacontext and returns the object created/updated. In my unit test I call first Add(), do some stuff and then call Update(). The problem is that Update() fails with the exception: System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use.. I understand the issue but want to know what to do about it? I've read a bit about how to handle multiple datacontext objects and from what I've heard this way is OK. I understand that the entity is still attached to the datacontext in Add() but I need to find out

What does LINQ-to-SQL Table<T>.Attach do?

只谈情不闲聊 提交于 2019-11-30 22:45:44
问题 What exactly does the LINQ-to-SQL method Table<T>.Attach() and Table<T>.AttachAll() and what is an example/situation for their proper usage? Also, please check out this related question: How to detach a LINQ-to-SQL data object from the DataContext's tracking mechanism? 回答1: It is really useful in multi-tier applications that serialize/deserialize data to other layers. Short version: Attach() tells DataContext the entity is not new (for insert ) but an updated entity that is meant to be

Using a DataContext static variable

核能气质少年 提交于 2019-11-30 22:44:23
I have recently inherited an ASP.Net app using Linq2SQL. Currently, it has its DataContext objects declared as static in every page, and I create them the first time I find they are null (singleton, sort of). I need comments if this is good or bad. In situations when I only need to read from the DB and in situations where i need to write as well. How about having just one DataContext instance for the entire application? One DataContext per application would perform badly, I'm afraid. The DataContext isn't thread safe, for starters, so even using one as a static member of a page is a bad idea.

Can I set a DataContext to a static class?

牧云@^-^@ 提交于 2019-11-30 19:57:06
I've a static class which reads information from the application assembly. I've declared it static since the class needs no instance declaration and will only ever be read directly from, application-wide. I have a control with several labels that I would like to use to display some of this information. How can I go about setting the controls DataContext equal to the class? Code: /// <summary> /// Class for Reading Program Information. /// </summary> public static class ProgramInfo { private static Assembly ProgramAssembly = Assembly.GetEntryAssembly( ); /// <summary> /// Get Program Name /// <

Using a DataContext static variable

巧了我就是萌 提交于 2019-11-30 18:04:25
问题 I have recently inherited an ASP.Net app using Linq2SQL. Currently, it has its DataContext objects declared as static in every page, and I create them the first time I find they are null (singleton, sort of). I need comments if this is good or bad. In situations when I only need to read from the DB and in situations where i need to write as well. How about having just one DataContext instance for the entire application? 回答1: One DataContext per application would perform badly, I'm afraid. The

KeyedCollection and d:DataContext Design Error

本小妞迷上赌 提交于 2019-11-30 15:58:04
See the update below for VS2013. When using a class as a d:DesignInstance that exposes a KeyedCollection<TKey, TItem> , the XAML designer complains with the following warning: The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation The problem can be reproduced with the following simple program: <Window x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns

LinqToSql static DataContext in a web application

霸气de小男生 提交于 2019-11-30 15:06:10
问题 In a web application that I have run across, I found the following code to deal with the DataContext when dealing with LinqToSQL public partial class DbDataContext { public static DbDataContext DB { get { if (HttpContext.Current.Items["DB"] == null) HttpContext.Current.Items["DB"] = new DbDataContext(); return (DbDataContext)HttpContext.Current.Items["DB"]; } } } Then referencing it later doing this: DbDataContext.DB.Accounts.Single(a => a.accountId == accountId).guid = newGuid; DbDataContext

LinqToSql static DataContext in a web application

谁说我不能喝 提交于 2019-11-30 13:48:43
In a web application that I have run across, I found the following code to deal with the DataContext when dealing with LinqToSQL public partial class DbDataContext { public static DbDataContext DB { get { if (HttpContext.Current.Items["DB"] == null) HttpContext.Current.Items["DB"] = new DbDataContext(); return (DbDataContext)HttpContext.Current.Items["DB"]; } } } Then referencing it later doing this: DbDataContext.DB.Accounts.Single(a => a.accountId == accountId).guid = newGuid; DbDataContext.DB.SubmitChanges(); I have been looking into best practices when dealing with LinqToSQL. I am unsure

Bind to parent DataContext within DataTemplate

若如初见. 提交于 2019-11-30 09:49:59
I'm trying to bind MenuItem's Command to command contained in UserControl.DataContext . I've found couple of similar question, but solution according to them is failing to me: <UserControl ...> <UserControl.Resources> <DataTemplate x:Key="TileItemStye"> <Grid Width="100" Height="100"> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="Remove" Command="{Binding DataContext.RemoveItem, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"> </MenuItem> </ContextMenu> </Grid.ContextMenu> </Grid> </DataTemplate> </UserControl.Resources> <Grid> <ListView ItemsSource="{Binding Path