Use SmallDateType in Framework-First Entity Framework

I cannot help but feel that I am missing something, but to this day I cannot find the answer.

I am making a model, the first entity structure, and have several properties set as DateTime. They translate DateTime into the database, but I would like to use SmallDateTime. In my case, switching to seconds and milliseconds just doesn't cost double memory for as many lines as I have.

Does anyone know a way in the first model to map a DateTime to a SmallDateTime DB field? As a last resort, I can generate DDL, replace everything and update the model from the database after - but I feel it is unpleasant!

Thanks in advance.

+3
source share
1 answer

This is kind of a tricky thing.

First of all, read this Model first article if you haven't already.

Then create a separate class library project with a custom implementation IGenerateActivityOutput.

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 change the database creation workflow.

  • Locate TablePerTypeStrategy.xaml in your file system.
  • Copy this file to the same folder with a different name. For example, TablePerTypeStrategy_smalldatetime.xaml. Open it with VS.
  • Change OutputGeneratorTypeon CsdlToSsdlAndMslActivityto "MyCSDLToSSDL.MyCsdlToSsdl, MyCSDLToSSDL". Double quotes are required.
  • Change the property of the database generation workflow to "TablePerTypeStrategy_smalldatetime.xaml (VS)".
  • Try to create a database from the model.

, .:) , !

+3

Source: https://habr.com/ru/post/1750574/


All Articles