specifying the foreign key to the table programmatically by using fluentapi


hi is there any possibility to assign a foriegnkey to table using entity ....

pls see this question for clarification.. <a href="https://stackoverflow.com/questions/7119764/how-do-add-a-single-entry-for-two-tables-using-linq-to-entities" rel="nofollow">how do add a single entry for two tables using linq to entities</a>

let me explain clearly.....

i have a product table product_id product_name product_description category_id category table category_id category_name

unfortunately it is not posssible to specify the category_id as a foreign key at the time of creating table the entity table it self taken the category_id as a column not as a foreign key

so.. i want to specify this one as foreign key so.. is it possible to specify as a foreign key by programmatically can any one have sample code for specifying foreignkey using fluent api


If you're using EntityFramework 4.1, you can use the <a href="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx" rel="nofollow">Fluent API</a>, which allows you to specify key names and other association properties.


Here`s a sample, of how I did that in EF 4.1 CodeFirst. I think, you can use very similar aproach.

public class P { public string Id { get; set; } public string Name { get; set; } public string Desc { get; set; } public string CategoryId { get; set; } } public class C { public string Id { get; set; } public string Name { get; set; } } public class ProductsContext : DbContext { public DbSet<P> Products { get; set; } public DbSet<C> Categories { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<P>().HasRequired(p => p.CategoryId).WithRequiredDependent(); } }

In the OnModelCreating, you tell context, how want to set relations, even if there are no such relations in DB.


  • codeigniter pagination link is not working when i select the link in the view?
  • invalid literal for int() with base 10: 'aa',,I'm not trying to pass number why am I
  • get_posts doesn't work but query_posts does (Wordpress)
  • Date format with codeigniter
  • Difference between add_filter versus add_action
  • Google sheets custom function error “Result was not a number”
  • Display a table in a foreach loop with database values
  • jQuery: Apply CSS on Ajax content
  • *ngFor is not working with dynamic values
  • specifying the foreign key to the table programmatically by using fluentapi
  • CakePHP3.4: How to send a json object response?
  • Simple_form input as both text and autocomplete
  • Outputting a posts categories as a class
  • inserting image base on description
  • how to bind data to listbox in wp7
  • How to filter many2one res.parner field in odoo
  • Merging two backbone collection and models into one object using underscore
  • Meteor form state not being saved
  • wp-query category and has tag
  • How to block days in UIDatePicker for iOS
  • zf2 tablegateway select columns by column name
  • Pylint rules : How to solve Undefined variable?
  • Yiic Grid view always giving live is not a function
  • How can i clean temp tables on mysql
  • if I want to find what's referencing an object in SQL Server, is searching syscomments comprehe
  • javascript / jquery scope differences between jQuery.each and normal for loop?
  • Many to Many in Linq using Dapper
  • Extending the Django 1.11 User Model
  • How to Model Ternary Relationship in CakePhp?
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Why joiner is not used after Sequence generator or Update statergy
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS