แสดงบทความที่มีป้ายกำกับ MVC แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ MVC แสดงบทความทั้งหมด

วันพุธที่ 22 เมษายน พ.ศ. 2558

ปัญหา MVC 5 ไม่สามารถ Log Off ออกได้ในบางครั้ง

สาเหตุน่าจะมาจากการเปลี่ยนชื่อ Cookie ของโปรแกรมเพื่อกัน ลอกอินข้ามแอพ แต่เป็นผลให้ ไม่สามรถ Log Off ออกได้ในบางครั้ง

แก้ได้ด้วยการ เปลี่ยน คำสั่ง ใน LogOff จาก

AuthenticationManager.SignOut();

เป็น

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

ลองดูแล้วก็ Work ดี จดไว้ก่อน

ref1 : http://stackoverflow.com/questions/29290120/cant-logoff-identity-mvc-5-sometimes
ref2 : http://stackoverflow.com/questions/28642284/asp-net-mvc-5-w-identity-2-2-0-log-off-not-working

วันพฤหัสบดีที่ 16 เมษายน พ.ศ. 2558

How to open MVC 2 or MVC 1 in Visual Studio 2013

Step 1: Right click on the project and click “Edit projectname.csproj”.
Step 2: In the opened file find the tag and from there delete the following GUID:
ASP.NET MVC 2: {F85E285D-A4E0-4152-9332-AB1D724D3325}
This actually works also for ASP.NET MVC 1 projects
ASP.NET MVC 1: {603c0e0b-db56-11dc-be95-000d561079b0}

Step 3: Save the file and reopen the solution in Visual Studio.


ref: http://dotnetdaily.net/tutorials/open-mvc-2-project-visual-studio-2012/

วันศุกร์ที่ 20 กุมภาพันธ์ พ.ศ. 2558

การเขียน MVC Codefirst Migration กับ Database ที่มีอยู่แล้ว ไม่ว่า Database นั้น จะสร้างด้วยวิธีไหน

สืบเนื่องจากย้ายโค้ดมาเขียนในโปรเจคใหม่ ทำให้ชื่อเปลี่ยน หลายอย่างเลยต้องเปลี่ยนด้วย ทีนี้ มีปัญหาต้องปรับปรุง Database เพิ่มเติม แต่ติดเมื่อใช้คำสั่ง Add-Migration จะเป็นการสร้าง Database ใหม่ทั้งหมดเลย ซึ่งไม่ใช่ ไม่สามารถไปรันได้ เพราะมี Database อยู่แล้ว เลยต้องทำดังนี้ 1. ใช้คำสั่ง Add-Migration InitialCreate -IgnoreChanges 2. Update-database เพื่อ Snapshort Database สถานะนี้ก่อน 3. จากนั้น ค่อยเพิ่ม Model หรือ Properties ที่ต้องการแล้วทำตามขั้นตอนปกติ ด้วยคำสั่ง Add-Migration ธรรมดา มีอีกวิธีหนึ่งก็คือ ใช้ วิธี add-migration แบบเดิม เพียงแต่ต้องไป Comment out Code ที่สร้างขึ้นมาเอง สำหรับตารางและความสัมพันธ์ที่สร้างไปแล้ว

วันศุกร์ที่ 12 ธันวาคม พ.ศ. 2557

การแก้ปัญหาหลังจากอัพเดท MVC แล้วเกิด Object reference not set to an instance of an object. โดย Microsoft.Owin.Security.Cookies.CookieAuthenticationProvider.Exception

ตรงจุดนี้ เราจะไม่สามารถรันหรือ Debug อะไรได้เลย  เนื่องจาก Error ตั้งแต่ Start เลย  ลองค้นดู
            
app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),

                        //**** This what I add ****//
                        OnException = context => {}
                }
            });

โดยการแสดงปัญหาจากตัว Authentication นั้น เอามาจากที่นี่

http://stackoverflow.com/questions/26327092/why-is-owin-throwing-a-null-exception-on-new-project

โดยเพิ่ม OnException เข้าไป จะพบกับ Error
Multiple object sets per type are not supported. The object
 sets 'ApplicationUsers' and 'Users' can both contain instances of type 
'CommandTracking.Models.User'.
แทน  ทีนี้ ลอง ค้นหาดูจะพบว่า ปัญหานี้ เกิดจาก ตัว Update ของ EntityFramework ที่เพิ่ม IDbset<TUser> Users ขึ้นมา ทำให้หากใน Project ของเรา มีตัว DbSet<ApplicationUser> เอาไว้ จะทำให้ เกิดการซ้ำซ้อนกันได้  แก้ไขโดย ลบ DbSet<ApplicationUser> ที่สร้างขึ้นเอง และ แก้ไขตัว db.<user> ที่เราใช้ ให้ไปใช้ db.Users แทน

วันพุธที่ 1 ตุลาคม พ.ศ. 2557

ปัญหา Cache จากการใช้ Entity Framework 6

สืบเนื่องมาจากมีข้อมูลอยู่ชุดหนึ่ง  ซึ่งพยายามที่จะอัพเดทมัน แต่ดูเหมือนอัพเดทได้อันเดียว อันที่เหลือ มีค่าแปลกๆ แม้จะเทสแล้วก็ตาม ลอง Debug ดู พบว่า มันจำค่าเดิม  โดยที่ไม่ได้สร้างลง Cache เอาไว้ หลังจากหาอยู่พักหนึ่ง ก็พบว่า  ถ้าใช้ .Find ในโปรเจ็ค  คำสั่งนี้ จะเรียกจาก แคช ก่อนเสมอ

ทำให้ค่าบางตัวที่ไม่ต้องการแคชเพี้ยนไป  แก้ได้ด้วยการใช้คำสั่ง  พวก .Single .First .Where แทน การใช้ .Find

แต่ถ้าหากตัวไหน ต้องการ Performance  ก็ให้ใช้ .Find ไป