locked
EF Code First database table creation error with mysql RRS feed

  • Question

  • I want to create a table with varchar() variable type fields

    but it creates only text() variable type fields?

    my domain class as below


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;

    namespace test.Domain
    {
        [Serializable]
        public class Agent
        {
            [Key,Required]
            [MaxLength(32)]
            public string AgentID { get; set; }
            [ConcurrencyCheck]
            [MaxLength(100)]
            public string AgentName { get; set; }
            [MaxLength(100)]
            public string AgentDescription { get; set; }
            public DateTime CheckingTime { get; set; }
        }
    }

    My Context file as below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MySql.Data.Entity;
    using System.Data.Entity.ModelConfiguration.Conventions;
    using test.Domain;
    using System.Data.Entity;

    namespace test.Infrastructure
    {
        public class testContext : DbContext
        {

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

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                try
                {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
                modelBuilder.Entity<Agent>().HasKey(a => new { a.AgentID });
                }
                catch (Exception exp)
                {
                    throw exp;
                }
            }
        }
    }

    when I run this I get an error as below

    BLOB/TEXT column 'AgentID' used in key specification without a key length

    And as I found all the table field I assign as string will generate only text() variable fields in database

    can any one help me for this?

    Sunday, May 13, 2012 1:18 PM

Answers

  • Hi Delush,

    Welcome to MSDN Forum.

    You can use Data Annotation to specify the database type which you want to map to. Below is a demo, please refer to it.

    class Test
        {
            public int id { get; set; }
    
            [Column(TypeName="varchar")]
            public string name { get; set; }
        }

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Allen_MSDN Saturday, May 19, 2012 6:50 AM
    Monday, May 14, 2012 6:07 AM

All replies