Навигация

Итоги года

Другие ссылки


Реклама

Счётчики


<- Предыдущие записи Следующие записи ->

Cannot add an entity with a key that is already in use

04.09.2008 Четверг 13:14

Related to: .Net 3.5, C#, LINQ, LINQ-to-SQL

Got the following error:

Cannot add an entity with a key that is already in use.

at the following line:

Context.SubmitChanges();

This error usually happens when the database and the Context object are out-of-sync. This may happen when:

a. You change database outside of the Context object. For example by directly executing command using SqlCommand class.

b. When you use more than one Context object to update database.

To solve this issue, either always use the same Context object to update database, or refresh all Context objects you use after a change in database made through one of them or oustide any Context object. To achieve this just re-create the object:

MyContext context;
...
context.Dispose();
context = new MyContext();


However, this will most likely discard all unsubmitted changes made through the Context object updated in such a manner. So you have to accomodate for the changes before refreshing your context objects.

Доктор, меня все игнорируют (нет комментариев)

Converting IEnumerable to EntitySet

03.09.2008 Среда 18:59

Software: .Net 3.5 or later

To convert IEnumerable to EntitySet use the following extension method:

private static EntitySet<T> ToEntitySet<T>(this IEnumerable<T> source) where T : class
{
var es = new EntitySet<T>();
es.AddRange(source);
return es;
}

Доктор, меня все игнорируют (нет комментариев)

Reading connection string from app.config

03.09.2008 Среда 14:40

Software: .Net, C#

This is how to read connection string from a configuration file (web.config, app.config). Assume you have the following in your configuration file:

<configuration>
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=localhost;Initial Catalog=master;Persist Security Info=True" />
</connectionStrings>
<configuration>
In order to read this string within C# code do this:

1. Add reference to System.Configuration.dll

2.Use this code:

string connString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

Доктор, меня все игнорируют (нет комментариев)

Error 0x80070005 at regsvr32

27.08.2008 Среда 16:18

Trying to register a COM dll in Vista Buisness SP1 with the following command:

regsvr32 [name of the dll]

I got the following error:

The module "[name of the dll]" was loaded but the call to DllRegisterServer failed with error code 0x80070005.

Resultion: Start command line in "Run as administrator" mode before issuing this command.

Комментариев: 2

LINQ to SQL and Unions

22.08.2008 Пятница 17:50

For some reason I was getting the following error:

Specified cast is not valid

when trying to use LINQ to SQL in the following way:

var q1 = from p in Context.Posts
... blah ...
select new SearchResult
{
...
};

var q2 = q1.Union
(
from c in Context.PostComments
... blah ...
select new SearchResult
{
...
}
);

return q2.ToArray();
The error was happening at the last line. Turned out the following fixes it:

var q1 = from p in Context.Posts
... blah ...
select new SearchResult
{
...
};

var q2 = from c in Context.PostComments
... blah ...
select new SearchResult
{
...
};

return q2.ToArray().Union(q2.ToArray());

Доктор, меня все игнорируют (нет комментариев)

Windows: 32 vs 64 bit

22.08.2008 Пятница 16:39

To find out what version of Windows (32 or 64 bit) your computer is running, type the following in the command prompt:

winver

A window appears that shows information about the operating system installed. If you don't see words "64 bit" anywhere then it's 32-bit Windows.

Доктор, меня все игнорируют (нет комментариев)

Magic expression

20.08.2008 Среда 15:31

What will be the result of the following expression in C#?

Math.Floor ((31.4 - Math.Floor(31.4)) * 60)

If your answer is 24 you are wrong. Give it a try. The result is not that obvious.

Доктор, меня все игнорируют (нет комментариев)

System.Core.dll grayed out

18.08.2008 Понедельник 14:40

I was trying to add a reference to assembly System.Core.dll in my project. However the dll's name was grayed out in the Add Reference dialog box. The reason is that System.Core.dll can only be added to projects whose target framework is .Net 3.5. Target framework can be changed in the project's properties.

Доктор, меня все игнорируют (нет комментариев)

Multiple development web servers

31.07.2008 Четверг 15:03

Software: Visual Studio 2008

You may have noticed that when you debug a web project that resides in a solution that contains other web projects, Visual Studio starts multiple development web servers — one for each web project in the solution. Sometimes it may be annoying (especially when you have somewhat 30 web projects). To turn this "feature" off do the following for each project:

- open the project's property grid
- set "Always Start When Debugging" property to false

Доктор, меня все игнорируют (нет комментариев)

Locating files in Solution Explorer

04.07.2008 Пятница 19:07

Software: Microsoft Visual Studio 2008

When studying unfamiliar code in Visual Studio and looking at contents of a file, have you ever wondered how to locate the same file in the Solution explorer window? That may not be easy at all if the solution has lots of projects and scores of files in them, and the way you got to open the file in the editor was through navigating symbol definitions (F12). Well, here's the remedy. For me it was real revelation since it's been many years that I've been suffering in such situations, trying to locate file either manually (by looking through all the projects in the solution) or by searching the physical media (folders on the hard drive) for the file name.

In Visual Studio:

Go to Tools —> Options —> Projects and Solutions —> General -> check Track Active Item in Solution Explorer.

This will make Visual Studio keep editor window and item selected in Solution explorer in sync. That is, if at ant time you switch from one file to another in editor window, solution explorer will find and highlight corresponding file.

Thank God this annoying issue is gone for good.

Courtesy this blog

Комментариев: 1
<- Предыдущие записи Следующие записи ->