Creating Unique Index with Entity Framework 6.1 fluent API

NOTE: Relevant to EF 6 You can use IndexAttribute as mentioned but with Fluent API instead of DataAnnotations which will do the trick: modelBuilder .Entity<Person>() .Property(t => t.Name) .HasColumnAnnotation( “Index”, new IndexAnnotation(new IndexAttribute(“IX_Name”) { IsUnique = true })); Unfortunately there is no other way to create unique indexes using Fluent API. There is an open … Read more

How do I map a char property using the Entity Framework 4.1 “code only” fluent API?

Char is not valid primitive type for entity framework = entity framework doesn’t map it. If you check CSDL reference you will see list of valid types (char is not among them). Database char(1) is translated as string (SQL to CSDL translation). Char is described as non-unicode string with fixed length 1. The only ugly … Read more

How to subclass str in Python

Overwriting __new__() works if you want to modify the string on construction: class caps(str): def __new__(cls, content): return str.__new__(cls, content.upper()) But if you just want to add new methods, you don’t even have to touch the constructor: class text(str): def duplicate(self): return text(self + self) Note that the inherited methods, like for example upper() will … Read more

Can you monkey patch methods on core types in Python?

No, you cannot. In Python, all data (classes, methods, functions, etc) defined in C extension modules (including builtins) are immutable. This is because C modules are shared between multiple interpreters in the same process, so monkeypatching them would also affect unrelated interpreters in the same process. (Multiple interpreters in the same process are possible through … Read more

Conditional Builder Method Chaining Fluent Interface

What I’d do is have NinjaBuilder keep the operations as a list of delegates, rather than applying them, and only apply them when .Build is called. This would allow you to make them conditional: public class NinjaBuilder { List<Action<Ninja>> builderActions = new List<Action<Ninja>>(); public Ninja Build() { var ninja = new Ninja(); builderActions.ForEach(ba => ba(ninja)); … Read more

Fluent interfaces and inheritance in C#

Try to use some Extension methods. static class FluentManager { public static T WithFirstName<T>(this T person, string firstName) where T : FluentPerson { person.FirstName = firstName; return person; } public static T WithId<T>(this T customer, long id) where T : FluentCustomer { customer.ID = id; return customer; } } class FluentPerson { public string FirstName … Read more

Entity Framework Code First Fluent Api: Adding Indexes to columns

After Migrations was introduced in EF 4.3 you can now add indexes when modifying or creating a table. Here is an excerpt from the EF 4.3 Code-Based Migrations Walkthrough from the ADO.NET team blog namespace MigrationsCodeDemo.Migrations { using System.Data.Entity.Migrations; public partial class AddPostClass : DbMigration { public override void Up() { CreateTable( “Posts”, c => … Read more

PHP method chaining or fluent interface?

It’s rather simple, really. You have a series of mutator methods that all return the original (or other) object. That way, you can keep calling methods on the returned object. <?php class fakeString { private $str; function __construct() { $this->str = “”; } function addA() { $this->str .= “a”; return $this; } function addB() { … Read more