Wednesday 31 October 2012

TFS Force Update Branch

In Tfs you can force update branch without loosing all history

1, Check out for edit branch that you want to update
2, Override all files from your working branch
3, Open the solution in your VS
4, Build to make sure that is working
5. Commit changes
6, Execute merge command
7, Commit changes

Done

Tuesday 30 October 2012

EF Set database object as read only



In addition to connecting with a read-only user, there are a few other things you can do to your DbContext.
public class MyReadOnlyContext : DbContext
{
    // Use ReadOnlyConnectionString from App/Web.config
    public MyContext()
        : base("Name=ReadOnlyConnectionString")
    {
    }

    // Don't expose Add(), Remove(), etc.
    public DbQuery<Customer> Customers
    {
        get
        {
            // Don't track changes to query results
            return Set<Customer>().AsNoTracking();
        }
    }

    public override void SaveChanges()
    {
        // Throw if they try to call this
        throw new InvalidOperationException("This context is read-only.");
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Need this since there is no DbSet<Customer> property
        modelBuilder.Entity<Customer>();
    }
}

Friday 26 October 2012

Small code snippents - ObjectToString


Sometimes is useful to display your object.
I wanted to display not only one item from object but whole object. Those can be quite big and not pleasant to display.

I have been pointed to the way, why not display it as xml.
and from this this came idea for this static serialize object.


So far i had no problem with this class. There is no exception trap, in order to let me know when some code has failed.

Nice class that I am using 

 public static string SerializeToString(object obj)
  {
   XmlSerializer serializer = new XmlSerializer(obj.GetType());

   using (StringWriter writer = new StringWriter())
   {
    serializer.Serialize(writer, obj);

    return writer.ToString();
   }
  }

Sunday 21 October 2012

VB.NET What are: Structures

Structures

Structures can be defined as a tool for handling a group of logically related data items. They are user-defined and provide a method for packing together data of different types. Structures are very similar to Classes. Like Classes, they too can contain members such as fields and methods. The main difference between classes and structures is, classes are reference types and structures are value types. In practical terms, structures are used for smaller lightweight objects that do not persist for long and classes are used for larger objects that are expected to exist in memory for long periods. We declare a structure in Visual Basic .NET with the Structure keyword.  
There are storage and performance advantages with Structures, often they are negligible.

Classes

Preffered by me.

Sources for this article:
http://msdn.microsoft.com/en-us/library/aa289521%28v=vs.71%29.aspx
http://www.startvbdotnet.com/oop/structure.aspx
and also we have nice article in code project
http://www.codeproject.com/Articles/8607/Using-Structures-in-VB-NET

Wednesday 17 October 2012

How to find out EF EntityValidationErrors

Entitiy Framework find out EntityValidationErrors

Often when I am working with EF I am caught by error in validation when saving data.

When you have something like :

 var user = DbContext.Logins.FirstOrDefault(x => x.Email == userName);
 if(user !=null){
   user.LastLoginDate = DateTime.Now;
   DbContext.SaveChanges();
 }

And when saving into the database I get exception:
{"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."}

So How to find out this?

surround the code with try catch and catch exception: DbEntityValidationException

try
{
    // code...

    context.SaveChanges();
}
catch (DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}


Source for this article:
stack overflow: http://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert