Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Annotations seems doesn't work in derived class #7007

Closed
BigVan opened this issue Nov 12, 2016 · 5 comments
Closed

Data Annotations seems doesn't work in derived class #7007

BigVan opened this issue Nov 12, 2016 · 5 comments

Comments

@BigVan
Copy link

BigVan commented Nov 12, 2016

Hi~
When I am using EF core to create MySQL table (using MySQL.Data.EntityFrameworkCore Package), I found some problems that Data Annotations [Column( TypeName = "varchar(200)" ] seems doesn't work.

Steps to reproduce

Here is my code:

public class BasicObject
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { set; get; }

        public string Name { set; get; }

        [Column(TypeName = "varchar(200)")]
        public string Description { set; get; }
}

public class Item : BasicObject
{
        public int Type { set; get; }

        public double Price { set; get; }
        
        public string Image { set; get; }
}

public class DBManager : DbContext
{
    public DBManager(DbContextOptions<DBManager> options)
    : base(options)
    { }

    public DbSet<Item> Items { get; set; }
  
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Item>().ToTable("Items");       
    }
}

When the database created, the type of column 'Description' is still varchar(255).

However, when I use 'Fluent API' as follow:

modelBuilder.Entity<Item>().ToTable("Items")
                        .Property(b=>b.Description).HasColumnType("varchar(200)");

It can change the column type to varchar(200).

So, Is this a bug ?

Further technical details

Visual studio version: 2015-community update3

@rowanmiller
Copy link
Contributor

What version of EF Core are you using?

@BigVan
Copy link
Author

BigVan commented Nov 16, 2016

MySQL.Data.EntityFrameworkCore (7.0.6-IR31)

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Nov 22, 2016

@BigVan This should have been fixed in 1.0.1. Update Microsoft.EntityFrameworkCore.Relational to 1.0.1 and try again.
If it still fails try updating further to 1.1.0, though it will probably require updating other components, depending on what you are using, see https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-net-core-1-1/

@kdazzle
Copy link

kdazzle commented Dec 7, 2016

I'm getting this too - my annotations don't seem to work for SQLite, but they're fine through the fluent api.

here are my dependencies:

"dependencies": {
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Relational": "1.1.0",
    "Microsoft.EntityFrameworkCore.Relational.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Sqlite": "1.1.0",
    "Microsoft.EntityFrameworkCore.Sqlite.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
  },

Or am I using the wrong annotation library? I couldn't find any mention of the one to use in the documentation, but I saw System.ComponentModel.DataAnnotations being used in some of the tests.

Here is an example class:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;

namespace Foo.models
{
    public class Foo
    {
        public int Id { get; set; }

        [Required, MaxLength(512)]
        public string Name { get; set; }
    }
}

@AndriySvyryd
Copy link
Member

This is a known issue: data annotations don't work on UWP, see #5471

@AndriySvyryd AndriySvyryd removed their assignment Apr 13, 2017
@divega divega removed this from the 2.0.0-preview1 milestone May 8, 2017
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants