2

After following the steps for mapping an existing table to a model, I realized the table had some dependencies.

For the last three times that helped, I just droped the whole DBContext and started again.

I would like to just update my context for accommodate another table, which relates to the current ones.

My current context is:

public partial class CompanyContext : DbContext
{
    public CompanyContext() : base("name=CompanyContext")
    {
    }

    public virtual DbSet<company> companies { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<company>()
            .Property(e => e.name)
            .IsUnicode(false);

        modelBuilder.Entity<company>()
            .Property(e => e.symbol)
            .IsUnicode(false);

        modelBuilder.Entity<company>()
            .Property(e => e.address)
            .IsUnicode(false);

        modelBuilder.Entity<company>()
            .Property(e => e.zipcode)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<company>()
            .Property(e => e.cnpj)
            .IsFixedLength()
            .IsUnicode(false);
    }
}

A company's record has id_city which is not even appearing in the DbContext. But it is there in the model:

[Table("company")]
public partial class company
{
    public int id { get; set; }

    [Required]
    [StringLength(70)]
    public string name { get; set; }

    [StringLength(10)]
    public string symbol { get; set; }

    [StringLength(80)]
    public string address { get; set; }

    [StringLength(8)]
    public string zipcode { get; set; }

    public int id_city { get; set; }

    public int id_segment { get; set; }

    [StringLength(14)]
    public string cnpj { get; set; }

    [Column(TypeName = "date")]
    public DateTime? open_date { get; set; }
}

I would like to add the table cities to this context, what should I do? Is there any wizard for helping it?

I thought it could be just add something like:

public virtual DbSet<city> cities { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<city>()
        .Property(e => e.name)
        .IsUnicode(false);
}

but it doesn't work.

4
  • Which version of EF are you using ? EF6 or EF7 ? Commented Apr 17, 2017 at 18:56
  • I am using version 5, does it matter? I mean, has this behavior changed accross versions? Commented Apr 17, 2017 at 19:14
  • 1
    No, there is no wizzard. There's a thing called EntityFramework Reverse POCO Generator, but what you did should be enough. What's the problem? "It doesn't work." isn't really informative. Commented Apr 17, 2017 at 19:22
  • I am sorry, I don't know exactly what happened. Before it was just passing by, without returning any records, now it is. I am sorry, don't know what has changed actually Commented Apr 17, 2017 at 19:31

1 Answer 1

2

Adding property like

public virtual DbSet<company> companies { get; set; }

To the context tells EF that you have a table that will hold records of type company. This one line is enough and table will use standard mapping so table name will be companies.

You need to do the same for cities.

public virtual DbSet<city> cities { get; set; }

You also need to add virtual property City to the company class if you want to have relationship in the database and remove id_city field. If you want id of related city to be exposed explicitly on the company class , you have to either use naming convention that EF follows, ie name it cityId or add fluent mapping for it.

Sign up to request clarification or add additional context in comments.

3 Comments

I am giving it a try again
Following your approach I got a proper error which helped me to figure out what was happening: The specified cast from a materialized 'System.Byte' type to the 'System.Int32' type is not valid. It was not blowing up before.
I mean, I thought I had made this before, but it was not returning any records, now I just made again, following your procedure, with lead to the same code (I guess) and it worked, sorry, don't know what I have done before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.