SapientGuardian.EntityFrameworkCore.MySql icon indicating copy to clipboard operation
SapientGuardian.EntityFrameworkCore.MySql copied to clipboard

Cannot use Table attribute to map table name.

Open redplane opened this issue 7 years ago • 2 comments

Hi there,

I'm currently facing an issue that I cannot map object to table name. For example. I have an object:

    [Table("tram")]
    public class Station
    {
        /// <summary>
        /// Id of station.
        /// </summary>
        [Column("ID")]
        public string Id { get; set; }

        /// <summary>
        /// Name of station.
        /// </summary>
        [Column("name")]
        public string Name { get; set; }

        /// <summary>
        /// Name of station type.
        /// </summary>
        [Column("typename")]
        public string TypeName { get; set; }

        /// <summary>
        /// Coordinate of station.
        /// </summary>
        [Column("name")]
        public string Coordinate { get; set; }

        /// <summary>
        /// Address of station.
        /// </summary>
        [Column("diachi")]
        public string Address { get; set; }

        /// <summary>
        /// Area which station belongs to.
        /// </summary>
        [Column("area")]
        public string Area { get; set; }

        /// <summary>
        /// Phone number of station monitor.
        /// </summary>
        [Column("phone")]
        public string Phone { get; set; }
    }

When I did a query, I received an exception: MySql.Data.MySqlClient.MySqlException: Table 'localdb.Station' doesn't exist

Is it a bug or my wrong implementation ?

Thank you,

redplane avatar May 08 '17 07:05 redplane

This seems to be a bug. I tried using an attribute to specify the table name and it was ignored for EnsureCreated as well. I'll look into this when I can. In the mean time, I think you should be able to use the fluent API, like

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Station>()
            .ToTable("tram");
    }

SapientGuardian avatar May 08 '17 20:05 SapientGuardian

Yes, I've tried this method and it worked :) But please take a look when you have time.

Thank you,

redplane avatar May 09 '17 02:05 redplane