havietduc91 / dormitory

Manage dormitory
0 stars 0 forks source link

Các lỗi hay gặp trong dự án #5

Open havietduc91 opened 8 years ago

havietduc91 commented 8 years ago
Object reference not set to an instance of an object

bug123

1. Lỗi null repo trong lớp Base.

Cách giải quyết:

Bước 1: Khởi tạo repo trong base bằng instance tương ứng.

Bước 2: Tạo một repo riêng để quản lý các nghiệp vụ mở rộng.

Bước 3: Gắn giá trị repo này từ instance của Repo trong Base.

Code ví dụ: Trong TruongService khai báo 2 dòng sau

repository = new TruongRepository(this.context);
truongRepository = (TruongRepository)repository;

Nguyên nhân của việc này là vì

  1. Trong lớp base hiện đang có 1 repo Ở bên lớp kế thừa có 1 repo. Khi chạy thì chỉ thằng repo ở thằng kế thừa là được khởi tạo. Còn thằng trong lớp base thì ko

Fix: repository = new TruongRepository(this.context); truongRepository = (TruongRepository)repository;

Trong ServiceBase mình có 1 repo với Interface là IRepo, cái này phục vụ cho các phương thức Base. Ở bên ngoài khi khởi tạo service thì ta phải khởi tạo repo này chính là 1 thể hiện của Repo cụ thể, khi đó repo ở base được khởi tạo. Ở bên ngoài mình cần định nghĩa 1 repo chính xác với Repo mình mở rộng ra, nên mình lại định nghĩa 1 repo riêng và gán giá trị cho nó. Khi đó ở bên ngoài đối với những cái mà mở rộng them thì sử dung repo định nghĩa, còn những cái mặc định thì giao cho thằng repo base.

Em lưu ý tới kiểu của nó là Interface. Em thấy trong lớp base kiểu của repo là IRepo, còn ở ngoài mình có ITruongRepo là mở rộng từ IRepo Khi khởi tạo repo = new TruongRepo thì có nghĩa repo thực tế là 1 thể hiện của TruongRepo rồi, nhưng kiểu của nó thì nó chỉ quan tâm tới interface IRepo thôi. Còn truongRepo thì lại quan tâm tới ITruongRepo, nên là phải ép kiểu thằng repo về kiểu TruongRepo thì mới lấy được Interface tương ứng của nó, vì TruongRepo cũng implement cái ITruongRepo. Còn việc khởi tạo ở đâu cái gì không quan trọng, mà chỉ quan trọng là cái gì cần phải khởi tạo để lấy được thông tin tương ứng.

havietduc91 commented 8 years ago
The number of primary key values passed must match number of primary key values defined on the entity.

bug

2. Lỗi liên quan tới việc truy vấn theo khoá, khi mà truyền giá trị id, nhưng trong thực tế thì bảng đó có nhiều hơn 1 khoá.

Cách giải quyết:

Bước 1: Định nghĩa mảng chứa giá trị của từng khoá.

Bước 2: Truyền giá trị này vào hàm GetById(); trên controller để lấy ra Entity tương ứng.

Code ví dụ:

object[] pk = new object2 { id, maTruong };
HeDaoTao heDaoTao = _heDaoTaoService.GetByID(pk);

Khi em thực hiện xóa 1 item của 1 trong các phần quản lý (hệ đào tạo, ..., lớp), trừ trường hoặc vào API sau: http://107.167.186.232:36/api/HeDaoTao/GetHeDaoTao/2 Thì sẽ báo lỗi ở hàm DbSet.Find(id); Báo lỗi sau: An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code The number of primary key values passed must match number of primary key values defined on the entity. Không biết là do gì nhỉ, em đã thử vào trong BGQLKTXEntities.edmx xóa tạm các model, rồi update model from DB sau đó add lại model vừa xóa mà vẫn báo lỗi này

Nguyên nhân: Trong thiết kế để khoá là Id và Mã Trường. Thì mình dùng lệnh find nó sẽ generate ra thấy ko đủ tham số để định vị duy nhất nên nó bắt mình phải nhập chính xác nó mới đồng ý Đó là nguyên nhân của cái lỗi.

Fix: Cần truyền đủ param khi xóa và update

havietduc91 commented 8 years ago
An error occurred while updating the entries. See the inner exception for details

http://stackoverflow.com/questions/15466854/an-error-occurred-while-updating-the-entries-see-the-inner-exception-for-detail

Click "View Detail..." a window will open where you can expand the "Inner Exception" my guess is that when you try to delete the record there is a reference constraint violation. The inner exception will give you more information on that so you can modify your code to remove any references prior to deleting the record.

bug3

havietduc91 commented 8 years ago
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
.net The underlying provider failed on Open

http://stackoverflow.com/questions/1391503/a-network-related-or-instance-specific-error-occurred-while-establishing-a-conne

Tìm vào file Web.config và xem lại config

havietduc91 commented 8 years ago
Lỗi khi yêu cầu Datetime không được NULL

Lúc khai báo thêm dấu ?: public DateTime NgaySinh { get; set; } -> public DateTime ? NgaySinh { get; set; }