Database entity model was generated with warnings or errors (asp.net)

Got some errors when trying to generate a DB entity model for a ASP.net MVC website. The error message was “The model was generated with warnings or errors. Please see the Error List for more details. These issues must be fixed before running your application”.

This turned to be caused by some tables that was lacking a primary key. Once a primary key was set on these tables the model could be generated successfully.

Convert float string to int c#

Converting a string to a integer seems to be trivial. But there could be endless of formats and styles on the string so it’s not always that easy that it’s seems.

If you have a string like “629.00” and want convert it to a int like 629. You can do like this:

int.Parse( ("629.00").Replace('.',','), NumberStyles.Float)

But if you try the code above with for example value “3187.50” instead you get this error message:

vid System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
vid System.Int32.Parse(String s, NumberStyles style)
vid System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
vid System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
vid Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
vid System.Threading.ThreadHelper.ThreadStart_Context(Object state)
vid System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

Then you need to do like:

(int) double.Parse("3187.50".Replace('.', ','), NumberStyles.Float)

View current sql queries on MSSQL database

To view current sql queries running on sql database you can use this sql script to get current query and elapsed time.

SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext

I have only tested the script Microsoft SQL Server 2012 and therefore I’m not sure if it’s working on MySQL.

Abstract InjectionMemberElement object cannot be created

Case: Azure webapp. Solution with multiple projects that uses unity.

Working fine on localhost but not when published, getting the message:

An error occurred creating the configuration section handler for unity: An abstract InjectionMemberElement object cannot be created. Please specify a concrete type.”

Unity is defined in the web.config as:

<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
</configuration>

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
...
</unity

Isolved it by renaming the unity tags to “unityX” so I suspect that the IIS already has loaded unity from another referenced project in the solution.

After the renaming it doesn’t work locally cause it then can’t find and load unity. But that’s another issue…

 

Entity type is not part of the model for current context

The entity type <Type> is not part of the model for the current context.

ExceptionType:
System.InvalidOperationException
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
vid System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
vid System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
vid System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()
--- Slut på stackspårningen från föregående plats där ett undantag utlöstes ---
vid System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
vid System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
vid System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
vid System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()
--- Slut på stackspårningen från föregående plats där ett undantag utlöstes ---
vid System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
vid System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
vid System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
vid System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

Cause:
Wrong meta data in the MVC entity framework connection string. Had multiple entity models to seperate databases in the project. But had copied the connection strings to the other databases and changed the database settings and credentials. But forgot to update the entity model names in the meta data section of the connectionstring.

Solution:
Update database model names in the connection strings.

Linq select one id with flattened list (example)

If you have an object with an id property and another property with a list of objects (has inheritance) and wants to get a new list with the inherited list flattened and keep the id. You can do like this:

public class Test
{
private void TestMethod()
{

Category _cat1 = new Category();
_cat1.Color = "Blue";

Category _cat2 = new Category();
_cat2.Color = "Green";

Category _cat3 = new Category();
_cat3.Color = "Red";

Category _cat4 = new Category();
_cat4.Color = "Yellow";

Category _cat5 = new Category();
_cat5.Color = "Lime";

List list1 = new List();
list1.Add(_cat1);
list1.Add(_cat2);
list1.Add(_cat3);

List list2 = new List();
list2.Add(_cat4);
list2.Add(_cat5);

List _listCar = new List();
_listCar.Add(new Car() { Id = 1, Categories = list1 });
_listCar.Add(new Car() { Id = 2, Categories = list2 });

var item = (from x in _listCar
.SelectMany(x => x.Categories, (Ids, Cars) =>
new { Ids, Cars })
select new
{
Id = x.Ids.Id,
Cat = x.Cars.Color
}).ToList();

var aba = item;

}
}
public class Car
{
public int Id { get; set; }
public List Categories { get; set; }

}
public class Category
{
public string Color { get; set; }
}
}

Local IIS failed to map the path ‘/’

Got an exception of System.InvalidOperationException: “Failed to map the path ‘/'” when starting the site on a local IIS.

System.Web.HttpRuntime.HostingInit(HostingEnvironmentFlags hostingFlags, PolicyLevel policyLevel, Exception appDomainCreationException) +556

[HttpException (0x80004005): Det gick inte att mappa sökvägen /.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +579
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +118
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +730

——
With my modest knowledge about hosting a site on a local IIS the solution turned out to be really simple.

1. Start a new command prompt in administrator mode.
2. Run command: “iisreset”

TADA!

Windows shortcut show desktop

In order to create a shortcut that shows the desktop you can create a shortcut in the quick launch section. Here is how:

1. Open the quick launch folder.
2. Create a new text file.
3. Copy and paste the following text in to the new file:
[Shell]
Command=2
IconFile=explorer.exe,3
[Taskbar]
Command=ToggleDesktop

4. Save the file.
5. Rename the file to “ShowDesktop.scf”.

Note! .scf files are used for Windows explorer commands.