Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Tuesday, August 22, 2017

Dotnet Core 1.1 to 2.0 conversion

Microsoft recently released Dotnet core 2.0.  I was eager to convert my Core 1.1 code so I jumped right in.  Just a bit of background on what I was dealing with (all Core 1.1):

- Around 18 class libraries
- Two MVC Core websites
- Entity Framework Core
- Identity Authentication
- MailKit (for SMTP email)

It took me probably four (4) hours to do the migration.  Not too painful, but I wanted to share several of the other websites that I bookmarked to help me get through it all.  Hopefully these will help you as well.





I think the issue that caused me the most pain (i.e. changes) was the Identity stuff.  It was a large number of changes, but just working to determine the differences between what the Core 1.1 template originally added vs. what Core 2.0 template had (which BTW, the 2.0 template still used Core 1.1 except for ASP.NET).

I was also able to remove MailKit totally once SmtpClient support came back to Dotnet Core.

Best of luck!

Friday, April 21, 2017

Entity Framework Core: Migrate and update a repository in class library

As of April 2017, Entity Framework Core (and Visual Studio 2017) does not support having your database repository in a separate class library.  Here are the steps I followed in order to work around this.  This was done successfully even with multiple repositories (i.e. more than one DbContext).

Hope this can help others as it took me awhile to get it right.  I did these steps for more than one DbContext and database so I'm sure this works as of the time I wrote this post.

Here is what I did to get things working nicely:

Added nuget packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.Tools

Added Program.cs to the class library project:
public class Program
{
 public static void Main(string[] args)
 {

 }

 public class xxxxContextFactory : IDbContextFactory<xxxxDbContext>
 {
  public xxxxDbContext Create(DbContextFactoryOptions options)
  {
   //
   // this is only used for data migrations and db updates; the connection
   // string is not used for production
   //

   var optionsBuilder = new DbContextOptionsBuilder<xxxxDbContext>();
   optionsBuilder.UseSqlServer("Server=.;Database=xxxxDatabase;Trusted_Connection=True;MultipleActiveResultSets=true");

   return new xxxxDbContext(optionsBuilder.Options);
  }
 }
}


Added property group to .csproj:
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

Set xxxxRepositoryModel as startup project.

Set the Package Manager Console "Default project" to your repository project.

Open Package Manager Console and then:
   PM> cd xxxxRepositoryModel
   PM> Add-Migration Initial -context xxxxDbContext
   PM> Update-Database -c xxxxDbContext

Note: If this is a brand new repository based on an EXISTING database and you have done the Package Manager command, Scaffold-DbContext, then you will need to:

  1. After you perform an Add-Migration...
  2. Modify your Migrations/yyyymmddhhmmss_Initial.cs file and...
  3. Completely empty the Up and Down methods
  4. Then continue to perform the Update-Database
  5. This gives you a clean starting point from which to make new database model changes (migrations) in the future.


Thursday, May 9, 2013

Call stored procedure with parameters using Entity Framework 5 and return scalar

Entity Framework 5 using a DbContext
bool result = myDbContext.Database.SqlQuery<bool>(
   "myProc @param1, @param2",
   new SqlParameter("param1", id),
   new SqlParameter("param2", name))
   .First();

Sunday, May 15, 2011

MVC 3, Razor, and Entity Framework Code-First

Here is a good tutorial (and downloadable PDF) on using MVC 3, Razor, and the EF Code-First (Code-First CPT 5 Release).

Visit Tutorial

I think the best part about the tutorial is how Code-First makes it really simple to set up pure POCO objects for data access.

Monday, July 19, 2010

Entity Framework: How to log generated SQL in ASP.NET

If you want to log/view the generated T-SQL from your Entity Framework context instances to the output window in Visual Studio create a TextWriter derivation and use it like so:

using (MyEntityContext db = new MyEntityContext())
{
  db.Log = new OutputDebugWriter();
  ...
}

class OutputDebugWriter : System.IO.TextWriter
{
   public override void Write(char[] buffer, int index, int count)
   {
      System.Diagnostics.Debug.Write(new string(buffer, index, count));
   }

   public override void Write(string value)
   {
      System.Diagnostics.Debug.Write(value);
   }

   public override Encoding Encoding
   {
      get { return System.Text.Encoding.Default; }
   }
}

Tuesday, December 29, 2009

Nested web.config files and connectionStrings (entry has already been added)

When using the Entity Framework (EF) recently, I ran into a situation where I received the following error message:
The entry 'myConnectionstring' has already been added...web.config line: 47
After investigating I found out this was due to my use of web.config files in nested directories. Here is the situation...

I have a reusable EF DAL (data access layer) that is used by several web services on a single web site. Several of these web services are in sub-folders with their own web.config files. This allows me to have custom settings for each service as well as being able to easily xcopy an entire directory and know I have all the correct settings. For the EF I have a connectionStrings key in the config files. In addition to the nested web.config files, I have the top-level config which also has the EF connectionStrings key.

When I tried to access a web service I would get the error above because .NET was merging the web.config files and found duplicate connectionString keys. To fix this I just had to add the following element within the connectionStrings section, directly before my key:
<remove name="myConnectionString" />
To facilitate knowing I can easily xcopy any web service, it is good practice to add this to all the web.config files, even the top-level one.

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...