Visual Studio 2010 Database Projects

I’ve never used a database project in a Visual Studio solution before. In most of the places I’ve worked, the database is managed by full time DBA’s, who deal in sql scripts taylored to make specific schema updates.

So i thought I would have a look. BLARG.

How about this gem in the generated deployment script? (And yes, the option to recreate the database was turned off now that you ask…)

IF (DB_ID(N'$(DatabaseName)') IS NOT NULL)
BEGIN
    ALTER DATABASE [$(DatabaseName)]
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    DROP DATABASE [$(DatabaseName)];
END

GO
PRINT N'Creating $(DatabaseName)...'
GO
CREATE DATABASE [$(DatabaseName)]
    ON
    PRIMARY(NAME = [Bonky], FILENAME = N'$(DefaultDataPath)Bonky.mdf')
    LOG ON (NAME = [Bonky_log], FILENAME = N'$(DefaultLogPath)Bonky_log.ldf')
    COLLATE Latin1_General_CI_AS
GO

Or the that changing the deployment Action to “create script and Deploy” installs the schema in the Master catalog? (and yes another database was selected in the “target database name” – it overwrote that silently when the deployment ran)

Ok the last issue does pop up a dialog, saying it will deploy to master at some point, but after changing the deployment target you would think…

Nah.

[UPDATE]

Ok not as bad as it first looked – the generated deploy script is isnsitive to the target – i.e. it only generates the update – so when I changed the databsae name from the one I was working with to the production one which it didn’t have access too it generated the DB drop/create statements. Pretty unpleasant but not as quite as illogical.

Pointing it at the dev database (which is up to date) generates a deploy script that does nothing apart from check the satate of the DB.

[UPDATE 2]

No this tool does not work for me. Next issue was – check solution into version control, drag out on another machine and use the “Database Projec”t to build a deployment script. Not sure what happened, but the script it built was missing more than half the tables. The individual scripts were still on the project but were not added to the new database. A “Visual Studio” schema compare between the project and the generated db was able to add the missing tables and indexes etc. but Just Not Good Enough.

I might shell out for Redgate tools that I have used before and been completely happy with.

Posted in VS2010 | Tagged | Leave a comment

New Info Format

I’ve a been working on the plan to add cheat sheets for the major technologies I work with. I think it would be worth adding example code and projects. Rather than hosting the code on my site I’m going to try using a git repo hosted on Bitbucket.org.

What is more interesting is that it may work both as another off site code backup too, With the added advantage of being able to share projects amongst collaborators.

I’ll use the example hosting as a test.

For the actual cheat sheets I have started, I’m thinking of using pdf’s rather than pages in WordPress going forward.

The nest set Im planning are on silverlight. If it works for that i’ll move the old stuff over too.

Posted in Uncategorized | Leave a comment

I’m Back

So that was a little odd. I’ve been working as a permi for a bit. My wife and I moved back to Scotland, mostly to be nearer to friends and family.

I couldn’t get any contract work up here and found it slow even to find a permanent position. I will say that once the new financial year started, the number of responses to job inquiries ramped up. So for future reference look in march – may.

The job I finally accepted required no less than 4 interviews after the basic telephone screening.

My wife however seemed to end up having an interview, followed by an interminable wait while the organisation she had applied to, decided to reorganise. This on more than one occasion.

So with wages being much less north of the border, both of us needed to be in work to cover the bills.

Luck presented an opportunity for a contract back in London, and payed enough that it would cover all the bills plus the flat rental. So it’s back down to the smoke for us.

Also the odds on Sheila getting work down here are much higher.

Now that we have set up the move it seems oddly like going home.

Posted in Uncategorized | Leave a comment

WAS net.tcp

Another reminder.

If you are using WAS to host a WCF service using net.Tcp, make sure the net.tcp listener and net.tcp port sharing services are set to run on the deployment machine. They are disabled by default.

Connection refused is the outcome otherwise.

Posted in WCF | Tagged , , | Leave a comment

ApplicationPoolIdentity and SQL Server Logins

Something I keep forgetting.

IIS on 2008R2 has created a special class of user to represent the security principal for code running in an app pool.

In the IIS manager the Identity is shown as ApplicationPoolIdentity. The actual user for an application pool, say the ASP.NET v4.0 one is IIS AppPool\ASP.NET v4.0
ie the source is “IIS AppPool” and the actual user name is the name of the app pool.

So if you are using windows integrated login with Sql server, on 2008R2 you can set up a login as above, but type it into the Login creation dialog, don’t use the search function, it will replace the “IIS AppPool” with your server name …. or I guess you can use it use it to confirm the login name if you remember to change the source part of the login back after searching.

Posted in Server 2008 | Tagged , , | Leave a comment

JQuery and Json with Asp.Net MVC 3

What a sad alphabet soup of acronyms this post has for a title…

Just a quick note on using Json ajax requests with asp.net mvc 3.

Adding a hnadler for a json request to a controller is easy, the return type is just JsonResult, and the controller’s Json method will handle the serialisation of the data.

The only gotcha is that by default, get will be disabled for json requests.

If you want to use the method withoutchanging that default, you cant use $.getJson, you need to use $.post instead.

$.post("<url>",
       "<data>,
       function (data,ststus,jqxhr) {
           /*handle the result*/
       },
       "json");

It is worth remembering that you can use the “Promise” interface from jquery 1.5 on to add an error handler, or it fails silently:

$.post("<url>",
       "<data>,
       function (data,ststus,jqxhr) {
           /*handle the result*/
       },
       "json").error(function(){/*error alert*/});

and if you need to parcel up the form for the data use $(“form selector”).serialize();

Posted in Uncategorized | Leave a comment

Entity Framework and POCOs

So I finally got around to trying Entity Framework with POCOs, where the domain model objects were hand coded and there was an aleady existing database.

The process was initially simple: run the tool that creates a model diagram of the database, turn off code generation, write an objectcontext, and some data repositories. Job nearly done.

The initial error message upon running the first unit test was a tad vague…

“Metadata information not found…” for one of the entities.

To cut a long story short, you need to check that everything matches exactly. Navigation properties and FKs are the most likely to give the error given. Once you find the worst errors, the messages in the exceptions tend to get much more helpfull, down to pointing out which property it cant find.

In my case it mostly involved editing the model to get the property names right, and more importantly remove some navigation properties that my domain model did not require.

It went from this:

Data model pretty much as generated from the existing database

Initial version of the data model after some minor edits

to this: (play spot the difference).

Edited model to remove all the inconsistencies

Edited model to remove all the inconsistencies

 

Posted in EF | Tagged , , | Leave a comment

Git and SSH and Pass Phrases

Continuing with the .Net development under VMware, One useful tip if you use the git bash shell (here) for your version control on windows, and have to access remote repositories over ssh, and you want to skip entering the passphrase at every operation, just do the following:

eval `ssh-agent`
ssh-add ~/.ssh/id_rsa

assuming you generated the key in the default location…

You will be asked to enter your pass phrase once and that is it for that session.

Note that this will only work for the shell you run it in, spawn a new one and you need to repeat the process.

On the mac, my ssh key pass phrase seems to be remembered, I’m guessing in the keystore but I haven’t checked that.

Posted in Uncategorized | Leave a comment

XAML Irritation

Can anyone explain the following:

<Style TargetType="{x:Type Button}"
          BasedOn="{StaticResource FontStyle}">
  <Setter Property="Template">
    <Setter.Value>
       <!-- wont work without
            respecifying the TargetType...?-->
       <ControlTemplate TargetType="{x:Type Button}">
         <Border Background="{TemplateBinding Property=Background}">
           <ContentPresenter/>
         </Border>
       </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

As in – why does the TargetType have to be restated in the ControlTemplate? The expected type of object in the control template is just Control, the error says that Control doesn’t have a Content property.

Posted in Uncategorized, WPF | Tagged , , | Leave a comment

Sum and Count in EF

It bugs me that I need to play around with casting in linq expressions just to make aggregate functions safe. For example:

decimal? total = storeDb.Carts
                   .Where(cart => cart.CartId == ShoppingCartId)
                   .Select(c => (decimal ?)c.Count * c.Album.Price)
                   .Sum();

See that (decimal ?) cast?

If you haven’t encountered it before, it’s because of what will happen if the result of the query is a null list:

The cast to value type ‘Decimal’ failed because the materialized value is null

It only happens when linq to sql gets involved, a linq object query against objects in an array (say) would be fine…

And no, neither of the fields in the database are nullable

Posted in EF | Tagged , , , | Leave a comment