3

I can't help but feel I am missing something but to this day I cannot find the answer.

I am doing a model-first entity framework and have a few properties set as DateTime. These translate to DateTime in the database - but I would like to use SmallDateTime. In my case, getting down to seconds and milliseconds just isn't worth the double storage for as many rows as I will have.

Does anyone know a way in the model-first environment to map DateTime to a SmallDateTime DB field? As a last hope, I can generate the DDL, replace all, and update the Model from the database after - but I feel that is obnoxious!

Thanks in advance.

1 Answer 1

3
+200

It's a kind of tricky thing.

First of all read this article Model first if you have not yet.

Then create separate class library project with custom IGenerateActivityOutput implementation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.Entity.Design.DatabaseGeneration.OutputGenerators;
using System.Activities;

namespace MyCSDLToSSDL
{
    public class MyCsdlToSsdl: IGenerateActivityOutput
    {
        private CsdlToSsdl _generator;
        public MyCsdlToSsdl()
        {
            _generator = new CsdlToSsdl();
        }

        public T GenerateActivityOutput<T>(OutputGeneratorActivity owningActivity, NativeActivityContext context, IDictionary<string, object> inputs) where T : class
        {
            var str = _generator.GenerateActivityOutput<T>(owningActivity, context, inputs) as string;

            return str.Replace("Type=\"datetime\"", "Type=\"smalldatetime\"") as T;
        }

    }
}

The next step is to modify database generation workflow.

  1. Locate TablePerTypeStrategy.xaml in your file system.
  2. Copy this file to the same folder with different name. TablePerTypeStrategy_smalldatetime.xaml for example. Open it with VS.
  3. Change OutputGeneratorType of CsdlToSsdlAndMslActivity to "MyCSDLToSSDL.MyCsdlToSsdl, MyCSDLToSSDL". Double quotes are required.
  4. Change database generation workflow property to "TablePerTypeStrategy_smalldatetime.xaml (VS)".
  5. Try to generate database from model.

Looks very much like a workaround but it works. :) Hope it helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the great answer. Both the way to do it as well as linking to an article for me to learn more. Bounty well deserved :)
Actually I am having one issue - I seem to be missing the Microsoft.Data.Entity.Design.DatabaseGeneration.OutputGenerators in my .NET references. I have the Entity Power-Pack. Is there anything else I would need?
It's in VS2010 install folder [install folder]\Microsoft Visual Studio 10.0\Common7\IDE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.