#

 

 

Entity Framework, Default, Types

 

Im folgenden habe ich unter ASP.Net Core 2 MVC eine Model-Klasse erstellt und diese per add-migration und update-database auf eine SQL-Server übertragen.

Dabei werden folgene Variablen in C# inklusive Default Vorgaben konvertiert auf einen SQL-Server

Wie man sieht werden die Default-Werte immer nur lokal gehalten.

Das heißt, Default Werte werden beim Code-Durchlaufen im Server zur Laufzeit erstellt, abhängig von den DataAnnotations.

Im Gegensatz hierzu werden die Default Einstellungen nicht berücksichtigt, wenn die SQL-Server Datenbank auch von anderen Anwendungen angesteuert wird.

 

[Key]

 

public int IDArtikel { get; set; }

 

 

[IDArtikel]       INT     IDENTITY (1, 1) NOT NULL,

[StringLength(50)]

//[StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]

[Display(Name = "Artikel Bezeichnung")]

public string Title { get; set; }

 

[Title]   NVARCHAR (50)   NULL,

public string Text_Content { get; set; }

 

[Text_Content]    NVARCHAR (MAX)  NULL

public int IDProduktgruppe { get; set; }

 

[IDProduktgruppe] INT     NOT NULL,

[DefaultValue(true)]

public bool IsReduced { get; set; }

 

[IsReduced]       BIT     NOT NULL,

[DefaultValue(false)]

public bool istOK { get; set; }

 

[istOK]   BIT     NOT NULL,

[DefaultValue(1234567890)]

public int Zahl_int { get; set; } //int32 min=-2.147.483.648 max=2.147.483.647

 

[Zahl_int]INT     NOT NULL,

[DefaultValue(12345678901234567890)]

public long Zahl_long { get; set; } //int64 max=9.223.372.036.854.775.807;

 

[Zahl_long]       BIGINT  NOT NULL,

[DefaultValue(0.001f)]

public Single Zahl_single { get; set; }  //32bit-float min -3.402823 E38 max=3.402823 E38, Eingabe 0.0001f

 

 [Zahl_single]     REAL    NOT NULL,

[DefaultValue(0.000001f)]

public double Preis_double { get; set; }

 

[Preis_double]    FLOAT (53)      NOT NULL

 

[Required]      //not null

[DefaultValue(11.22)]

public decimal Preis_decimal { get; set; } //Euro Dollar usw

[Preis_decimal]   DECIMAL (18, 2) NOT NULL,

 

[DefaultValue(0.55)]

public float Zahl_float { get; set; }

 [Zahl_float]      REAL    NOT NULL,

   

 

//Datum

[DataType(DataType.Date)]

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

public DateTime EnrollmentDate { get; set; }

 

[EnrollmentDate]  DATETIME2 (7)   NOT NULL,

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;    //DefaultValue

using System.ComponentModel.DataAnnotations; //*[key]

using System.Linq;

using System.Threading.Tasks;

 

namespace ArtikelWeb.Models

{

    public class Artikel

    {

[Key]

 

public int IDArtikel { get; set; }

[StringLength(50)]

//[StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]

[Display(Name = "Artikel Bezeichnung")]

public string Title { get; set; }

 

public string Text_Content { get; set; }

public Produktgruppen Produktgruppe { get; set; }

public int IDProduktgruppe { get; set; }

 

[DefaultValue(true)]

public bool IsReduced { get; set; }

[DefaultValue(false)]

public bool istOK { get; set; }

 

[DefaultValue(1234567890)]

public int Zahl_int { get; set; } //int32 min=-2.147.483.648 max=2.147.483.647

[DefaultValue(12345678901234567890)]

public long Zahl_long { get; set; } //int64 max=9.223.372.036.854.775.807;

 

 

[DefaultValue(0.001f)]

public Single Zahl_single { get; set; }  //32bit-float min -3.402823 E38 max=3.402823 E38, Eingabe 0.0001f

[DefaultValue(0.000001f)]

public double Preis_double { get; set; }

 

[Required]      //not null

[DefaultValue(11.22)]

public decimal Preis_decimal { get; set; } //Euro Dollar usw

[DefaultValue(0.55)]

public float Zahl_float { get; set; }

 

//Datum

[DataType(DataType.Date)]

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

public DateTime EnrollmentDate { get; set; }

 

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

CREATE TABLE [dbo].[Test_Fields] (

    [IDArtikel]       INT     IDENTITY (1, 1) NOT NULL,

    [EnrollmentDate]  DATETIME2 (7)   NOT NULL,

    [IDProduktgruppe] INT     NOT NULL,

    [IsReduced]       BIT     NOT NULL,

    [Preis_decimal]   DECIMAL (18, 2) NOT NULL,

    [Preis_double]    FLOAT (53)      NOT NULL,

    [Text_Content]    NVARCHAR (MAX)  NULL,

    [Title]   NVARCHAR (50)   NULL,

    [Zahl_float]      REAL    NOT NULL,

    [Zahl_int]INT     NOT NULL,

    [Zahl_long]       BIGINT  NOT NULL,

    [Zahl_single]     REAL    NOT NULL,

    [istOK]   BIT     NOT NULL,

    CONSTRAINT [PK_Test_Fields] PRIMARY KEY CLUSTERED ([IDArtikel] ASC)

);

 

 

 

 

 

Bemerkung:

Wichtig ist für den Vorgang, dass man zunächts eine DbContext als Struktur des Datenmodells erstellt

using Microsoft.EntityFrameworkCore;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

 

namespace test04.Models

{

    public class app_DbContext : DbContext

    {

        public app_DbContext(DbContextOptions<app_DbContext> options)

            : base(options)

        { }

 

        public DbSet<Test_Fields> Test_Fields { get; set; }

 

        protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            modelBuilder.Entity<Test_Fields>().ToTable("Test_Fields");

           

        }

    }

}

 

 

 

Wichtig ist, dass man im Startup.cs die Verbindung zur Datenbank einträgt

using Microsoft.EntityFrameworkCore;

..

..

public void ConfigureServices(IServiceCollection services)

        {

            var connection = @"Server=(localdb)\mssqllocaldb;Database=Test04;Trusted_Connection=True;";

            services.AddDbContext<app_DbContext>(options => options.UseSqlServer(connection));

            services.AddMvc();

        }

 

 

 

Unter ASP.Net

 

Unter des SQL Server

Mobile

.

123movies