Entity Framework 6 GUID as primary key: Cannot insert the value NULL into column ‘Id’, table ‘FileStore’; column does not allow nulls

In addition to adding these attributes to your Id column: [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } in your migration you should change your CreateTable to add the defaultValueSQL property to your column i.e.: Id = c.Guid(nullable: false, identity: true, defaultValueSql: “newsequentialid()”), This will prevent you from having to manually touch your database … Read more

Reference type in C#

Both person and person2 are references, to the same object. But these are different references. So when you are running person2 = null; you are changing only reference person2, leaving reference person and the corresponding object unchanged. I guess the best way to explain this is with a simplified illustration. Here is how the situation … Read more

Capture the screen shot using .NET [duplicate]

It’s certainly possible to grab a screenshot using the .NET Framework. The simplest way is to create a new Bitmap object and draw into that using the Graphics.CopyFromScreen method. Sample code: using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)) using (Graphics g = Graphics.FromImage(bmpScreenCapture)) { g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy); } Caveat: This method doesn’t … Read more

Set timeout to an operation

You could run the operation in a separate thread and then put a timeout on the thread join operation: using System.Threading; class Program { static void DoSomething() { try { // your call here… obj.PerformInitTransaction(); } catch (ThreadAbortException) { // cleanup code, if needed… } } public static void Main(params string[] args) { Thread t … Read more

Enable CORS in Web API 2

CORS works absolutely fine in Microsoft.AspNet.WebApi.Cors version 5.2.2. The following steps configured CORS like a charm for me: Install-Package Microsoft.AspNet.WebApi.Cors -Version “5.2.2” // run from Package manager console In Global.asax, add the following line: BEFORE ANY MVC ROUTE REGISTRATIONS GlobalConfiguration.Configure(WebApiConfig.Register); In the WebApiConfig Register method, have the following code: public static void Register(HttpConfiguration config) { … Read more

How to mock static methods in c# using MOQ framework?

Moq (and other DynamicProxy-based mocking frameworks) are unable to mock anything that is not a virtual or abstract method. Sealed/static classes/methods can only be faked with Profiler API based tools, like Typemock (commercial) or Microsoft Moles (free, known as Fakes in Visual Studio 2012 Ultimate /2013 /2015). Alternatively, you could refactor your design to abstract … Read more

How do I accept an array as an ASP.NET MVC controller action parameter?

The default model binder expects this url: http://localhost:54119/Designs/Multiple?ids=24041&ids=24117 in order to successfully bind to: public ActionResult Multiple(int[] ids) { … } And if you want this to work with comma separated values you could write a custom model binder: public class IntArrayModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value … Read more

How to programmatically modify WCF app.config endpoint address setting?

Is this on the client side of things?? If so, you need to create an instance of WsHttpBinding, and an EndpointAddress, and then pass those two to the proxy client constructor that takes these two as parameters. // using System.ServiceModel; WSHttpBinding binding = new WSHttpBinding(); EndpointAddress endpoint = new EndpointAddress(new Uri(“http://localhost:9000/MyService”)); MyServiceClient client = new … Read more