Here's the situation. I have two table (Table 1 and Table 2) with the same entities and a class(ClassA) which was mapped to Table 1 but in some scenario I have to transfer the specific data from Table 1 to Table 2. How can I map the two table (Table 1 and Table 2) to ClassA using Entity Framework Code First?

  • Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question.
    – gnat
    Nov 18 '15 at 19:09
  • 1
    @Ron: Are you trying to copy some records from one table to another? Nov 18 '15 at 19:14
  • @Robert Harvey: Yes, I need to copy some records from one table to another without creating another class like ClassB.
    – Ron
    Nov 18 '15 at 19:18
  • 2
    Write a Linq query using a Select() that projects each entity from Table1 into a new entity in Table2, and write the resulting collection into Table2. Alternatively, execute a bit of SQL. Nov 18 '15 at 19:29
  • please have a look at this url dotnetfalcon.azurewebsites.net/… Jan 18 '18 at 11:24

Even though you're using Code First, it's important to remember that your C# entities are projections of your database at runtime. If you use schema generation, your database schema is a projection of your C# entity metadata.

That means the safest mapping is one-to-one. If you can live without schema generation, you can get away with multiple entities mapped to a database object. Conceptually, you can never get away with one entity mapped to multiple database objects. Sure, you might be able to temporarily do it by completely subverting the EF plumbing, but at that point you're not truly mapped in any meaningful sense. Why not step outside the EF world to do whatever needs doing and step back in when you're finished? Either that or modify your model slightly to accommodate EF's expectations.

  • Thanks Corbin, Yes I think I need to create another class (Class B) for the Table 2 and map like what I did to Table 1 map to Class A.
    – Ron
    Nov 19 '15 at 19:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.